cz_hwmgr.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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 "smu/smu_8_0_d.h"
  32. #include "smu8_fusion.h"
  33. #include "smu/smu_8_0_sh_mask.h"
  34. #include "smumgr.h"
  35. #include "hwmgr.h"
  36. #include "hardwaremanager.h"
  37. #include "cz_ppsmc.h"
  38. #include "cz_hwmgr.h"
  39. #include "power_state.h"
  40. #include "cz_clockpowergating.h"
  41. #define ixSMUSVI_NB_CURRENTVID 0xD8230044
  42. #define CURRENT_NB_VID_MASK 0xff000000
  43. #define CURRENT_NB_VID__SHIFT 24
  44. #define ixSMUSVI_GFX_CURRENTVID 0xD8230048
  45. #define CURRENT_GFX_VID_MASK 0xff000000
  46. #define CURRENT_GFX_VID__SHIFT 24
  47. static const unsigned long PhwCz_Magic = (unsigned long) PHM_Cz_Magic;
  48. static struct cz_power_state *cast_PhwCzPowerState(struct pp_hw_power_state *hw_ps)
  49. {
  50. if (PhwCz_Magic != hw_ps->magic)
  51. return NULL;
  52. return (struct cz_power_state *)hw_ps;
  53. }
  54. static const struct cz_power_state *cast_const_PhwCzPowerState(
  55. const struct pp_hw_power_state *hw_ps)
  56. {
  57. if (PhwCz_Magic != hw_ps->magic)
  58. return NULL;
  59. return (struct cz_power_state *)hw_ps;
  60. }
  61. static uint32_t cz_get_eclk_level(struct pp_hwmgr *hwmgr,
  62. uint32_t clock, uint32_t msg)
  63. {
  64. int i = 0;
  65. struct phm_vce_clock_voltage_dependency_table *ptable =
  66. hwmgr->dyn_state.vce_clock_voltage_dependency_table;
  67. switch (msg) {
  68. case PPSMC_MSG_SetEclkSoftMin:
  69. case PPSMC_MSG_SetEclkHardMin:
  70. for (i = 0; i < (int)ptable->count; i++) {
  71. if (clock <= ptable->entries[i].ecclk)
  72. break;
  73. }
  74. break;
  75. case PPSMC_MSG_SetEclkSoftMax:
  76. case PPSMC_MSG_SetEclkHardMax:
  77. for (i = ptable->count - 1; i >= 0; i--) {
  78. if (clock >= ptable->entries[i].ecclk)
  79. break;
  80. }
  81. break;
  82. default:
  83. break;
  84. }
  85. return i;
  86. }
  87. static uint32_t cz_get_sclk_level(struct pp_hwmgr *hwmgr,
  88. uint32_t clock, uint32_t msg)
  89. {
  90. int i = 0;
  91. struct phm_clock_voltage_dependency_table *table =
  92. hwmgr->dyn_state.vddc_dependency_on_sclk;
  93. switch (msg) {
  94. case PPSMC_MSG_SetSclkSoftMin:
  95. case PPSMC_MSG_SetSclkHardMin:
  96. for (i = 0; i < (int)table->count; i++) {
  97. if (clock <= table->entries[i].clk)
  98. break;
  99. }
  100. break;
  101. case PPSMC_MSG_SetSclkSoftMax:
  102. case PPSMC_MSG_SetSclkHardMax:
  103. for (i = table->count - 1; i >= 0; i--) {
  104. if (clock >= table->entries[i].clk)
  105. break;
  106. }
  107. break;
  108. default:
  109. break;
  110. }
  111. return i;
  112. }
  113. static uint32_t cz_get_uvd_level(struct pp_hwmgr *hwmgr,
  114. uint32_t clock, uint32_t msg)
  115. {
  116. int i = 0;
  117. struct phm_uvd_clock_voltage_dependency_table *ptable =
  118. hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
  119. switch (msg) {
  120. case PPSMC_MSG_SetUvdSoftMin:
  121. case PPSMC_MSG_SetUvdHardMin:
  122. for (i = 0; i < (int)ptable->count; i++) {
  123. if (clock <= ptable->entries[i].vclk)
  124. break;
  125. }
  126. break;
  127. case PPSMC_MSG_SetUvdSoftMax:
  128. case PPSMC_MSG_SetUvdHardMax:
  129. for (i = ptable->count - 1; i >= 0; i--) {
  130. if (clock >= ptable->entries[i].vclk)
  131. break;
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. return i;
  138. }
  139. static uint32_t cz_get_max_sclk_level(struct pp_hwmgr *hwmgr)
  140. {
  141. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  142. if (cz_hwmgr->max_sclk_level == 0) {
  143. smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxSclkLevel);
  144. cz_hwmgr->max_sclk_level = smum_get_argument(hwmgr->smumgr) + 1;
  145. }
  146. return cz_hwmgr->max_sclk_level;
  147. }
  148. static int cz_initialize_dpm_defaults(struct pp_hwmgr *hwmgr)
  149. {
  150. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  151. uint32_t i;
  152. struct cgs_system_info sys_info = {0};
  153. int result;
  154. cz_hwmgr->gfx_ramp_step = 256*25/100;
  155. cz_hwmgr->gfx_ramp_delay = 1; /* by default, we delay 1us */
  156. for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++)
  157. cz_hwmgr->activity_target[i] = CZ_AT_DFLT;
  158. cz_hwmgr->mgcg_cgtt_local0 = 0x00000000;
  159. cz_hwmgr->mgcg_cgtt_local1 = 0x00000000;
  160. cz_hwmgr->clock_slow_down_freq = 25000;
  161. cz_hwmgr->skip_clock_slow_down = 1;
  162. cz_hwmgr->enable_nb_ps_policy = 1; /* disable until UNB is ready, Enabled */
  163. cz_hwmgr->voltage_drop_in_dce_power_gating = 0; /* disable until fully verified */
  164. cz_hwmgr->voting_rights_clients = 0x00C00033;
  165. cz_hwmgr->static_screen_threshold = 8;
  166. cz_hwmgr->ddi_power_gating_disabled = 0;
  167. cz_hwmgr->bapm_enabled = 1;
  168. cz_hwmgr->voltage_drop_threshold = 0;
  169. cz_hwmgr->gfx_power_gating_threshold = 500;
  170. cz_hwmgr->vce_slow_sclk_threshold = 20000;
  171. cz_hwmgr->dce_slow_sclk_threshold = 30000;
  172. cz_hwmgr->disable_driver_thermal_policy = 1;
  173. cz_hwmgr->disable_nb_ps3_in_battery = 0;
  174. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  175. PHM_PlatformCaps_ABM);
  176. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  177. PHM_PlatformCaps_NonABMSupportInPPLib);
  178. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  179. PHM_PlatformCaps_DynamicM3Arbiter);
  180. cz_hwmgr->override_dynamic_mgpg = 1;
  181. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  182. PHM_PlatformCaps_DynamicPatchPowerState);
  183. cz_hwmgr->thermal_auto_throttling_treshold = 0;
  184. cz_hwmgr->tdr_clock = 0;
  185. cz_hwmgr->disable_gfx_power_gating_in_uvd = 0;
  186. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  187. PHM_PlatformCaps_DynamicUVDState);
  188. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  189. PHM_PlatformCaps_UVDDPM);
  190. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  191. PHM_PlatformCaps_VCEDPM);
  192. cz_hwmgr->cc6_settings.cpu_cc6_disable = false;
  193. cz_hwmgr->cc6_settings.cpu_pstate_disable = false;
  194. cz_hwmgr->cc6_settings.nb_pstate_switch_disable = false;
  195. cz_hwmgr->cc6_settings.cpu_pstate_separation_time = 0;
  196. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  197. PHM_PlatformCaps_DisableVoltageIsland);
  198. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  199. PHM_PlatformCaps_UVDPowerGating);
  200. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  201. PHM_PlatformCaps_VCEPowerGating);
  202. sys_info.size = sizeof(struct cgs_system_info);
  203. sys_info.info_id = CGS_SYSTEM_INFO_PG_FLAGS;
  204. result = cgs_query_system_info(hwmgr->device, &sys_info);
  205. if (!result) {
  206. if (sys_info.value & AMD_PG_SUPPORT_UVD)
  207. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  208. PHM_PlatformCaps_UVDPowerGating);
  209. if (sys_info.value & AMD_PG_SUPPORT_VCE)
  210. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  211. PHM_PlatformCaps_VCEPowerGating);
  212. }
  213. return 0;
  214. }
  215. static uint32_t cz_convert_8Bit_index_to_voltage(
  216. struct pp_hwmgr *hwmgr, uint16_t voltage)
  217. {
  218. return 6200 - (voltage * 25);
  219. }
  220. static int cz_construct_max_power_limits_table(struct pp_hwmgr *hwmgr,
  221. struct phm_clock_and_voltage_limits *table)
  222. {
  223. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)hwmgr->backend;
  224. struct cz_sys_info *sys_info = &cz_hwmgr->sys_info;
  225. struct phm_clock_voltage_dependency_table *dep_table =
  226. hwmgr->dyn_state.vddc_dependency_on_sclk;
  227. if (dep_table->count > 0) {
  228. table->sclk = dep_table->entries[dep_table->count-1].clk;
  229. table->vddc = cz_convert_8Bit_index_to_voltage(hwmgr,
  230. (uint16_t)dep_table->entries[dep_table->count-1].v);
  231. }
  232. table->mclk = sys_info->nbp_memory_clock[0];
  233. return 0;
  234. }
  235. static int cz_init_dynamic_state_adjustment_rule_settings(
  236. struct pp_hwmgr *hwmgr,
  237. ATOM_CLK_VOLT_CAPABILITY *disp_voltage_table)
  238. {
  239. uint32_t table_size =
  240. sizeof(struct phm_clock_voltage_dependency_table) +
  241. (7 * sizeof(struct phm_clock_voltage_dependency_record));
  242. struct phm_clock_voltage_dependency_table *table_clk_vlt =
  243. kzalloc(table_size, GFP_KERNEL);
  244. if (NULL == table_clk_vlt) {
  245. pr_err("Can not allocate memory!\n");
  246. return -ENOMEM;
  247. }
  248. table_clk_vlt->count = 8;
  249. table_clk_vlt->entries[0].clk = PP_DAL_POWERLEVEL_0;
  250. table_clk_vlt->entries[0].v = 0;
  251. table_clk_vlt->entries[1].clk = PP_DAL_POWERLEVEL_1;
  252. table_clk_vlt->entries[1].v = 1;
  253. table_clk_vlt->entries[2].clk = PP_DAL_POWERLEVEL_2;
  254. table_clk_vlt->entries[2].v = 2;
  255. table_clk_vlt->entries[3].clk = PP_DAL_POWERLEVEL_3;
  256. table_clk_vlt->entries[3].v = 3;
  257. table_clk_vlt->entries[4].clk = PP_DAL_POWERLEVEL_4;
  258. table_clk_vlt->entries[4].v = 4;
  259. table_clk_vlt->entries[5].clk = PP_DAL_POWERLEVEL_5;
  260. table_clk_vlt->entries[5].v = 5;
  261. table_clk_vlt->entries[6].clk = PP_DAL_POWERLEVEL_6;
  262. table_clk_vlt->entries[6].v = 6;
  263. table_clk_vlt->entries[7].clk = PP_DAL_POWERLEVEL_7;
  264. table_clk_vlt->entries[7].v = 7;
  265. hwmgr->dyn_state.vddc_dep_on_dal_pwrl = table_clk_vlt;
  266. return 0;
  267. }
  268. static int cz_get_system_info_data(struct pp_hwmgr *hwmgr)
  269. {
  270. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)hwmgr->backend;
  271. ATOM_INTEGRATED_SYSTEM_INFO_V1_9 *info = NULL;
  272. uint32_t i;
  273. int result = 0;
  274. uint8_t frev, crev;
  275. uint16_t size;
  276. info = (ATOM_INTEGRATED_SYSTEM_INFO_V1_9 *) cgs_atom_get_data_table(
  277. hwmgr->device,
  278. GetIndexIntoMasterTable(DATA, IntegratedSystemInfo),
  279. &size, &frev, &crev);
  280. if (crev != 9) {
  281. pr_err("Unsupported IGP table: %d %d\n", frev, crev);
  282. return -EINVAL;
  283. }
  284. if (info == NULL) {
  285. pr_err("Could not retrieve the Integrated System Info Table!\n");
  286. return -EINVAL;
  287. }
  288. cz_hwmgr->sys_info.bootup_uma_clock =
  289. le32_to_cpu(info->ulBootUpUMAClock);
  290. cz_hwmgr->sys_info.bootup_engine_clock =
  291. le32_to_cpu(info->ulBootUpEngineClock);
  292. cz_hwmgr->sys_info.dentist_vco_freq =
  293. le32_to_cpu(info->ulDentistVCOFreq);
  294. cz_hwmgr->sys_info.system_config =
  295. le32_to_cpu(info->ulSystemConfig);
  296. cz_hwmgr->sys_info.bootup_nb_voltage_index =
  297. le16_to_cpu(info->usBootUpNBVoltage);
  298. cz_hwmgr->sys_info.htc_hyst_lmt =
  299. (info->ucHtcHystLmt == 0) ? 5 : info->ucHtcHystLmt;
  300. cz_hwmgr->sys_info.htc_tmp_lmt =
  301. (info->ucHtcTmpLmt == 0) ? 203 : info->ucHtcTmpLmt;
  302. if (cz_hwmgr->sys_info.htc_tmp_lmt <=
  303. cz_hwmgr->sys_info.htc_hyst_lmt) {
  304. pr_err("The htcTmpLmt should be larger than htcHystLmt.\n");
  305. return -EINVAL;
  306. }
  307. cz_hwmgr->sys_info.nb_dpm_enable =
  308. cz_hwmgr->enable_nb_ps_policy &&
  309. (le32_to_cpu(info->ulSystemConfig) >> 3 & 0x1);
  310. for (i = 0; i < CZ_NUM_NBPSTATES; i++) {
  311. if (i < CZ_NUM_NBPMEMORYCLOCK) {
  312. cz_hwmgr->sys_info.nbp_memory_clock[i] =
  313. le32_to_cpu(info->ulNbpStateMemclkFreq[i]);
  314. }
  315. cz_hwmgr->sys_info.nbp_n_clock[i] =
  316. le32_to_cpu(info->ulNbpStateNClkFreq[i]);
  317. }
  318. for (i = 0; i < MAX_DISPLAY_CLOCK_LEVEL; i++) {
  319. cz_hwmgr->sys_info.display_clock[i] =
  320. le32_to_cpu(info->sDispClkVoltageMapping[i].ulMaximumSupportedCLK);
  321. }
  322. /* Here use 4 levels, make sure not exceed */
  323. for (i = 0; i < CZ_NUM_NBPSTATES; i++) {
  324. cz_hwmgr->sys_info.nbp_voltage_index[i] =
  325. le16_to_cpu(info->usNBPStateVoltage[i]);
  326. }
  327. if (!cz_hwmgr->sys_info.nb_dpm_enable) {
  328. for (i = 1; i < CZ_NUM_NBPSTATES; i++) {
  329. if (i < CZ_NUM_NBPMEMORYCLOCK) {
  330. cz_hwmgr->sys_info.nbp_memory_clock[i] =
  331. cz_hwmgr->sys_info.nbp_memory_clock[0];
  332. }
  333. cz_hwmgr->sys_info.nbp_n_clock[i] =
  334. cz_hwmgr->sys_info.nbp_n_clock[0];
  335. cz_hwmgr->sys_info.nbp_voltage_index[i] =
  336. cz_hwmgr->sys_info.nbp_voltage_index[0];
  337. }
  338. }
  339. if (le32_to_cpu(info->ulGPUCapInfo) &
  340. SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS) {
  341. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  342. PHM_PlatformCaps_EnableDFSBypass);
  343. }
  344. cz_hwmgr->sys_info.uma_channel_number = info->ucUMAChannelNumber;
  345. cz_construct_max_power_limits_table (hwmgr,
  346. &hwmgr->dyn_state.max_clock_voltage_on_ac);
  347. cz_init_dynamic_state_adjustment_rule_settings(hwmgr,
  348. &info->sDISPCLK_Voltage[0]);
  349. return result;
  350. }
  351. static int cz_construct_boot_state(struct pp_hwmgr *hwmgr)
  352. {
  353. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  354. cz_hwmgr->boot_power_level.engineClock =
  355. cz_hwmgr->sys_info.bootup_engine_clock;
  356. cz_hwmgr->boot_power_level.vddcIndex =
  357. (uint8_t)cz_hwmgr->sys_info.bootup_nb_voltage_index;
  358. cz_hwmgr->boot_power_level.dsDividerIndex = 0;
  359. cz_hwmgr->boot_power_level.ssDividerIndex = 0;
  360. cz_hwmgr->boot_power_level.allowGnbSlow = 1;
  361. cz_hwmgr->boot_power_level.forceNBPstate = 0;
  362. cz_hwmgr->boot_power_level.hysteresis_up = 0;
  363. cz_hwmgr->boot_power_level.numSIMDToPowerDown = 0;
  364. cz_hwmgr->boot_power_level.display_wm = 0;
  365. cz_hwmgr->boot_power_level.vce_wm = 0;
  366. return 0;
  367. }
  368. static int cz_tf_reset_active_process_mask(struct pp_hwmgr *hwmgr, void *input,
  369. void *output, void *storage, int result)
  370. {
  371. return 0;
  372. }
  373. static int cz_tf_upload_pptable_to_smu(struct pp_hwmgr *hwmgr, void *input,
  374. void *output, void *storage, int result)
  375. {
  376. struct SMU8_Fusion_ClkTable *clock_table;
  377. int ret;
  378. uint32_t i;
  379. void *table = NULL;
  380. pp_atomctrl_clock_dividers_kong dividers;
  381. struct phm_clock_voltage_dependency_table *vddc_table =
  382. hwmgr->dyn_state.vddc_dependency_on_sclk;
  383. struct phm_clock_voltage_dependency_table *vdd_gfx_table =
  384. hwmgr->dyn_state.vdd_gfx_dependency_on_sclk;
  385. struct phm_acp_clock_voltage_dependency_table *acp_table =
  386. hwmgr->dyn_state.acp_clock_voltage_dependency_table;
  387. struct phm_uvd_clock_voltage_dependency_table *uvd_table =
  388. hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
  389. struct phm_vce_clock_voltage_dependency_table *vce_table =
  390. hwmgr->dyn_state.vce_clock_voltage_dependency_table;
  391. if (!hwmgr->need_pp_table_upload)
  392. return 0;
  393. ret = smum_download_powerplay_table(hwmgr->smumgr, &table);
  394. PP_ASSERT_WITH_CODE((0 == ret && NULL != table),
  395. "Fail to get clock table from SMU!", return -EINVAL;);
  396. clock_table = (struct SMU8_Fusion_ClkTable *)table;
  397. /* patch clock table */
  398. PP_ASSERT_WITH_CODE((vddc_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
  399. "Dependency table entry exceeds max limit!", return -EINVAL;);
  400. PP_ASSERT_WITH_CODE((vdd_gfx_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
  401. "Dependency table entry exceeds max limit!", return -EINVAL;);
  402. PP_ASSERT_WITH_CODE((acp_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
  403. "Dependency table entry exceeds max limit!", return -EINVAL;);
  404. PP_ASSERT_WITH_CODE((uvd_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
  405. "Dependency table entry exceeds max limit!", return -EINVAL;);
  406. PP_ASSERT_WITH_CODE((vce_table->count <= CZ_MAX_HARDWARE_POWERLEVELS),
  407. "Dependency table entry exceeds max limit!", return -EINVAL;);
  408. for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++) {
  409. /* vddc_sclk */
  410. clock_table->SclkBreakdownTable.ClkLevel[i].GnbVid =
  411. (i < vddc_table->count) ? (uint8_t)vddc_table->entries[i].v : 0;
  412. clock_table->SclkBreakdownTable.ClkLevel[i].Frequency =
  413. (i < vddc_table->count) ? vddc_table->entries[i].clk : 0;
  414. atomctrl_get_engine_pll_dividers_kong(hwmgr,
  415. clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
  416. &dividers);
  417. clock_table->SclkBreakdownTable.ClkLevel[i].DfsDid =
  418. (uint8_t)dividers.pll_post_divider;
  419. /* vddgfx_sclk */
  420. clock_table->SclkBreakdownTable.ClkLevel[i].GfxVid =
  421. (i < vdd_gfx_table->count) ? (uint8_t)vdd_gfx_table->entries[i].v : 0;
  422. /* acp breakdown */
  423. clock_table->AclkBreakdownTable.ClkLevel[i].GfxVid =
  424. (i < acp_table->count) ? (uint8_t)acp_table->entries[i].v : 0;
  425. clock_table->AclkBreakdownTable.ClkLevel[i].Frequency =
  426. (i < acp_table->count) ? acp_table->entries[i].acpclk : 0;
  427. atomctrl_get_engine_pll_dividers_kong(hwmgr,
  428. clock_table->AclkBreakdownTable.ClkLevel[i].Frequency,
  429. &dividers);
  430. clock_table->AclkBreakdownTable.ClkLevel[i].DfsDid =
  431. (uint8_t)dividers.pll_post_divider;
  432. /* uvd breakdown */
  433. clock_table->VclkBreakdownTable.ClkLevel[i].GfxVid =
  434. (i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
  435. clock_table->VclkBreakdownTable.ClkLevel[i].Frequency =
  436. (i < uvd_table->count) ? uvd_table->entries[i].vclk : 0;
  437. atomctrl_get_engine_pll_dividers_kong(hwmgr,
  438. clock_table->VclkBreakdownTable.ClkLevel[i].Frequency,
  439. &dividers);
  440. clock_table->VclkBreakdownTable.ClkLevel[i].DfsDid =
  441. (uint8_t)dividers.pll_post_divider;
  442. clock_table->DclkBreakdownTable.ClkLevel[i].GfxVid =
  443. (i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
  444. clock_table->DclkBreakdownTable.ClkLevel[i].Frequency =
  445. (i < uvd_table->count) ? uvd_table->entries[i].dclk : 0;
  446. atomctrl_get_engine_pll_dividers_kong(hwmgr,
  447. clock_table->DclkBreakdownTable.ClkLevel[i].Frequency,
  448. &dividers);
  449. clock_table->DclkBreakdownTable.ClkLevel[i].DfsDid =
  450. (uint8_t)dividers.pll_post_divider;
  451. /* vce breakdown */
  452. clock_table->EclkBreakdownTable.ClkLevel[i].GfxVid =
  453. (i < vce_table->count) ? (uint8_t)vce_table->entries[i].v : 0;
  454. clock_table->EclkBreakdownTable.ClkLevel[i].Frequency =
  455. (i < vce_table->count) ? vce_table->entries[i].ecclk : 0;
  456. atomctrl_get_engine_pll_dividers_kong(hwmgr,
  457. clock_table->EclkBreakdownTable.ClkLevel[i].Frequency,
  458. &dividers);
  459. clock_table->EclkBreakdownTable.ClkLevel[i].DfsDid =
  460. (uint8_t)dividers.pll_post_divider;
  461. }
  462. ret = smum_upload_powerplay_table(hwmgr->smumgr);
  463. return ret;
  464. }
  465. static int cz_tf_init_sclk_limit(struct pp_hwmgr *hwmgr, void *input,
  466. void *output, void *storage, int result)
  467. {
  468. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  469. struct phm_clock_voltage_dependency_table *table =
  470. hwmgr->dyn_state.vddc_dependency_on_sclk;
  471. unsigned long clock = 0, level;
  472. if (NULL == table || table->count <= 0)
  473. return -EINVAL;
  474. cz_hwmgr->sclk_dpm.soft_min_clk = table->entries[0].clk;
  475. cz_hwmgr->sclk_dpm.hard_min_clk = table->entries[0].clk;
  476. level = cz_get_max_sclk_level(hwmgr) - 1;
  477. if (level < table->count)
  478. clock = table->entries[level].clk;
  479. else
  480. clock = table->entries[table->count - 1].clk;
  481. cz_hwmgr->sclk_dpm.soft_max_clk = clock;
  482. cz_hwmgr->sclk_dpm.hard_max_clk = clock;
  483. return 0;
  484. }
  485. static int cz_tf_init_uvd_limit(struct pp_hwmgr *hwmgr, void *input,
  486. void *output, void *storage, int result)
  487. {
  488. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  489. struct phm_uvd_clock_voltage_dependency_table *table =
  490. hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
  491. unsigned long clock = 0, level;
  492. if (NULL == table || table->count <= 0)
  493. return -EINVAL;
  494. cz_hwmgr->uvd_dpm.soft_min_clk = 0;
  495. cz_hwmgr->uvd_dpm.hard_min_clk = 0;
  496. smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxUvdLevel);
  497. level = smum_get_argument(hwmgr->smumgr);
  498. if (level < table->count)
  499. clock = table->entries[level].vclk;
  500. else
  501. clock = table->entries[table->count - 1].vclk;
  502. cz_hwmgr->uvd_dpm.soft_max_clk = clock;
  503. cz_hwmgr->uvd_dpm.hard_max_clk = clock;
  504. return 0;
  505. }
  506. static int cz_tf_init_vce_limit(struct pp_hwmgr *hwmgr, void *input,
  507. void *output, void *storage, int result)
  508. {
  509. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  510. struct phm_vce_clock_voltage_dependency_table *table =
  511. hwmgr->dyn_state.vce_clock_voltage_dependency_table;
  512. unsigned long clock = 0, level;
  513. if (NULL == table || table->count <= 0)
  514. return -EINVAL;
  515. cz_hwmgr->vce_dpm.soft_min_clk = 0;
  516. cz_hwmgr->vce_dpm.hard_min_clk = 0;
  517. smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxEclkLevel);
  518. level = smum_get_argument(hwmgr->smumgr);
  519. if (level < table->count)
  520. clock = table->entries[level].ecclk;
  521. else
  522. clock = table->entries[table->count - 1].ecclk;
  523. cz_hwmgr->vce_dpm.soft_max_clk = clock;
  524. cz_hwmgr->vce_dpm.hard_max_clk = clock;
  525. return 0;
  526. }
  527. static int cz_tf_init_acp_limit(struct pp_hwmgr *hwmgr, void *input,
  528. void *output, void *storage, int result)
  529. {
  530. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  531. struct phm_acp_clock_voltage_dependency_table *table =
  532. hwmgr->dyn_state.acp_clock_voltage_dependency_table;
  533. unsigned long clock = 0, level;
  534. if (NULL == table || table->count <= 0)
  535. return -EINVAL;
  536. cz_hwmgr->acp_dpm.soft_min_clk = 0;
  537. cz_hwmgr->acp_dpm.hard_min_clk = 0;
  538. smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetMaxAclkLevel);
  539. level = smum_get_argument(hwmgr->smumgr);
  540. if (level < table->count)
  541. clock = table->entries[level].acpclk;
  542. else
  543. clock = table->entries[table->count - 1].acpclk;
  544. cz_hwmgr->acp_dpm.soft_max_clk = clock;
  545. cz_hwmgr->acp_dpm.hard_max_clk = clock;
  546. return 0;
  547. }
  548. static int cz_tf_init_power_gate_state(struct pp_hwmgr *hwmgr, void *input,
  549. void *output, void *storage, int result)
  550. {
  551. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  552. cz_hwmgr->uvd_power_gated = false;
  553. cz_hwmgr->vce_power_gated = false;
  554. cz_hwmgr->samu_power_gated = false;
  555. cz_hwmgr->acp_power_gated = false;
  556. cz_hwmgr->pgacpinit = true;
  557. return 0;
  558. }
  559. static int cz_tf_init_sclk_threshold(struct pp_hwmgr *hwmgr, void *input,
  560. void *output, void *storage, int result)
  561. {
  562. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  563. cz_hwmgr->low_sclk_interrupt_threshold = 0;
  564. return 0;
  565. }
  566. static int cz_tf_update_sclk_limit(struct pp_hwmgr *hwmgr,
  567. void *input, void *output,
  568. void *storage, int result)
  569. {
  570. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  571. struct phm_clock_voltage_dependency_table *table =
  572. hwmgr->dyn_state.vddc_dependency_on_sclk;
  573. unsigned long clock = 0;
  574. unsigned long level;
  575. unsigned long stable_pstate_sclk;
  576. unsigned long percentage;
  577. cz_hwmgr->sclk_dpm.soft_min_clk = table->entries[0].clk;
  578. level = cz_get_max_sclk_level(hwmgr) - 1;
  579. if (level < table->count)
  580. cz_hwmgr->sclk_dpm.soft_max_clk = table->entries[level].clk;
  581. else
  582. cz_hwmgr->sclk_dpm.soft_max_clk = table->entries[table->count - 1].clk;
  583. clock = hwmgr->display_config.min_core_set_clock;
  584. if (clock == 0)
  585. pr_info("min_core_set_clock not set\n");
  586. if (cz_hwmgr->sclk_dpm.hard_min_clk != clock) {
  587. cz_hwmgr->sclk_dpm.hard_min_clk = clock;
  588. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  589. PPSMC_MSG_SetSclkHardMin,
  590. cz_get_sclk_level(hwmgr,
  591. cz_hwmgr->sclk_dpm.hard_min_clk,
  592. PPSMC_MSG_SetSclkHardMin));
  593. }
  594. clock = cz_hwmgr->sclk_dpm.soft_min_clk;
  595. /* update minimum clocks for Stable P-State feature */
  596. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  597. PHM_PlatformCaps_StablePState)) {
  598. percentage = 75;
  599. /*Sclk - calculate sclk value based on percentage and find FLOOR sclk from VddcDependencyOnSCLK table */
  600. stable_pstate_sclk = (hwmgr->dyn_state.max_clock_voltage_on_ac.mclk *
  601. percentage) / 100;
  602. if (clock < stable_pstate_sclk)
  603. clock = stable_pstate_sclk;
  604. } else {
  605. if (clock < hwmgr->gfx_arbiter.sclk)
  606. clock = hwmgr->gfx_arbiter.sclk;
  607. }
  608. if (cz_hwmgr->sclk_dpm.soft_min_clk != clock) {
  609. cz_hwmgr->sclk_dpm.soft_min_clk = clock;
  610. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  611. PPSMC_MSG_SetSclkSoftMin,
  612. cz_get_sclk_level(hwmgr,
  613. cz_hwmgr->sclk_dpm.soft_min_clk,
  614. PPSMC_MSG_SetSclkSoftMin));
  615. }
  616. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  617. PHM_PlatformCaps_StablePState) &&
  618. cz_hwmgr->sclk_dpm.soft_max_clk != clock) {
  619. cz_hwmgr->sclk_dpm.soft_max_clk = clock;
  620. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  621. PPSMC_MSG_SetSclkSoftMax,
  622. cz_get_sclk_level(hwmgr,
  623. cz_hwmgr->sclk_dpm.soft_max_clk,
  624. PPSMC_MSG_SetSclkSoftMax));
  625. }
  626. return 0;
  627. }
  628. static int cz_tf_set_deep_sleep_sclk_threshold(struct pp_hwmgr *hwmgr,
  629. void *input, void *output,
  630. void *storage, int result)
  631. {
  632. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  633. PHM_PlatformCaps_SclkDeepSleep)) {
  634. uint32_t clks = hwmgr->display_config.min_core_set_clock_in_sr;
  635. if (clks == 0)
  636. clks = CZ_MIN_DEEP_SLEEP_SCLK;
  637. PP_DBG_LOG("Setting Deep Sleep Clock: %d\n", clks);
  638. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  639. PPSMC_MSG_SetMinDeepSleepSclk,
  640. clks);
  641. }
  642. return 0;
  643. }
  644. static int cz_tf_set_watermark_threshold(struct pp_hwmgr *hwmgr,
  645. void *input, void *output,
  646. void *storage, int result)
  647. {
  648. struct cz_hwmgr *cz_hwmgr =
  649. (struct cz_hwmgr *)(hwmgr->backend);
  650. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  651. PPSMC_MSG_SetWatermarkFrequency,
  652. cz_hwmgr->sclk_dpm.soft_max_clk);
  653. return 0;
  654. }
  655. static int cz_tf_set_enabled_levels(struct pp_hwmgr *hwmgr,
  656. void *input, void *output,
  657. void *storage, int result)
  658. {
  659. return 0;
  660. }
  661. static int cz_tf_enable_nb_dpm(struct pp_hwmgr *hwmgr,
  662. void *input, void *output,
  663. void *storage, int result)
  664. {
  665. int ret = 0;
  666. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  667. unsigned long dpm_features = 0;
  668. if (!cz_hwmgr->is_nb_dpm_enabled) {
  669. PP_DBG_LOG("enabling ALL SMU features.\n");
  670. dpm_features |= NB_DPM_MASK;
  671. ret = smum_send_msg_to_smc_with_parameter(
  672. hwmgr->smumgr,
  673. PPSMC_MSG_EnableAllSmuFeatures,
  674. dpm_features);
  675. if (ret == 0)
  676. cz_hwmgr->is_nb_dpm_enabled = true;
  677. }
  678. return ret;
  679. }
  680. static int cz_nbdpm_pstate_enable_disable(struct pp_hwmgr *hwmgr, bool enable, bool lock)
  681. {
  682. struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
  683. if (hw_data->is_nb_dpm_enabled) {
  684. if (enable) {
  685. PP_DBG_LOG("enable Low Memory PState.\n");
  686. return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  687. PPSMC_MSG_EnableLowMemoryPstate,
  688. (lock ? 1 : 0));
  689. } else {
  690. PP_DBG_LOG("disable Low Memory PState.\n");
  691. return smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  692. PPSMC_MSG_DisableLowMemoryPstate,
  693. (lock ? 1 : 0));
  694. }
  695. }
  696. return 0;
  697. }
  698. static int cz_tf_update_low_mem_pstate(struct pp_hwmgr *hwmgr,
  699. void *input, void *output,
  700. void *storage, int result)
  701. {
  702. bool disable_switch;
  703. bool enable_low_mem_state;
  704. struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
  705. const struct phm_set_power_state_input *states = (struct phm_set_power_state_input *)input;
  706. const struct cz_power_state *pnew_state = cast_const_PhwCzPowerState(states->pnew_state);
  707. if (hw_data->sys_info.nb_dpm_enable) {
  708. disable_switch = hw_data->cc6_settings.nb_pstate_switch_disable ? true : false;
  709. enable_low_mem_state = hw_data->cc6_settings.nb_pstate_switch_disable ? false : true;
  710. if (pnew_state->action == FORCE_HIGH)
  711. cz_nbdpm_pstate_enable_disable(hwmgr, false, disable_switch);
  712. else if (pnew_state->action == CANCEL_FORCE_HIGH)
  713. cz_nbdpm_pstate_enable_disable(hwmgr, true, disable_switch);
  714. else
  715. cz_nbdpm_pstate_enable_disable(hwmgr, enable_low_mem_state, disable_switch);
  716. }
  717. return 0;
  718. }
  719. static const struct phm_master_table_item cz_set_power_state_list[] = {
  720. { .tableFunction = cz_tf_update_sclk_limit },
  721. { .tableFunction = cz_tf_set_deep_sleep_sclk_threshold },
  722. { .tableFunction = cz_tf_set_watermark_threshold },
  723. { .tableFunction = cz_tf_set_enabled_levels },
  724. { .tableFunction = cz_tf_enable_nb_dpm },
  725. { .tableFunction = cz_tf_update_low_mem_pstate },
  726. { }
  727. };
  728. static const struct phm_master_table_header cz_set_power_state_master = {
  729. 0,
  730. PHM_MasterTableFlag_None,
  731. cz_set_power_state_list
  732. };
  733. static const struct phm_master_table_item cz_setup_asic_list[] = {
  734. { .tableFunction = cz_tf_reset_active_process_mask },
  735. { .tableFunction = cz_tf_upload_pptable_to_smu },
  736. { .tableFunction = cz_tf_init_sclk_limit },
  737. { .tableFunction = cz_tf_init_uvd_limit },
  738. { .tableFunction = cz_tf_init_vce_limit },
  739. { .tableFunction = cz_tf_init_acp_limit },
  740. { .tableFunction = cz_tf_init_power_gate_state },
  741. { .tableFunction = cz_tf_init_sclk_threshold },
  742. { }
  743. };
  744. static const struct phm_master_table_header cz_setup_asic_master = {
  745. 0,
  746. PHM_MasterTableFlag_None,
  747. cz_setup_asic_list
  748. };
  749. static int cz_tf_power_up_display_clock_sys_pll(struct pp_hwmgr *hwmgr,
  750. void *input, void *output,
  751. void *storage, int result)
  752. {
  753. struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
  754. hw_data->disp_clk_bypass_pending = false;
  755. hw_data->disp_clk_bypass = false;
  756. return 0;
  757. }
  758. static int cz_tf_clear_nb_dpm_flag(struct pp_hwmgr *hwmgr,
  759. void *input, void *output,
  760. void *storage, int result)
  761. {
  762. struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
  763. hw_data->is_nb_dpm_enabled = false;
  764. return 0;
  765. }
  766. static int cz_tf_reset_cc6_data(struct pp_hwmgr *hwmgr,
  767. void *input, void *output,
  768. void *storage, int result)
  769. {
  770. struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
  771. hw_data->cc6_settings.cc6_setting_changed = false;
  772. hw_data->cc6_settings.cpu_pstate_separation_time = 0;
  773. hw_data->cc6_settings.cpu_cc6_disable = false;
  774. hw_data->cc6_settings.cpu_pstate_disable = false;
  775. return 0;
  776. }
  777. static const struct phm_master_table_item cz_power_down_asic_list[] = {
  778. { .tableFunction = cz_tf_power_up_display_clock_sys_pll },
  779. { .tableFunction = cz_tf_clear_nb_dpm_flag },
  780. { .tableFunction = cz_tf_reset_cc6_data },
  781. { }
  782. };
  783. static const struct phm_master_table_header cz_power_down_asic_master = {
  784. 0,
  785. PHM_MasterTableFlag_None,
  786. cz_power_down_asic_list
  787. };
  788. static int cz_tf_program_voting_clients(struct pp_hwmgr *hwmgr, void *input,
  789. void *output, void *storage, int result)
  790. {
  791. PHMCZ_WRITE_SMC_REGISTER(hwmgr->device, CG_FREQ_TRAN_VOTING_0,
  792. PPCZ_VOTINGRIGHTSCLIENTS_DFLT0);
  793. return 0;
  794. }
  795. static int cz_tf_start_dpm(struct pp_hwmgr *hwmgr, void *input, void *output,
  796. void *storage, int result)
  797. {
  798. int res = 0xff;
  799. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  800. unsigned long dpm_features = 0;
  801. cz_hwmgr->dpm_flags |= DPMFlags_SCLK_Enabled;
  802. dpm_features |= SCLK_DPM_MASK;
  803. res = smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  804. PPSMC_MSG_EnableAllSmuFeatures,
  805. dpm_features);
  806. return res;
  807. }
  808. static int cz_tf_program_bootup_state(struct pp_hwmgr *hwmgr, void *input,
  809. void *output, void *storage, int result)
  810. {
  811. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  812. cz_hwmgr->sclk_dpm.soft_min_clk = cz_hwmgr->sys_info.bootup_engine_clock;
  813. cz_hwmgr->sclk_dpm.soft_max_clk = cz_hwmgr->sys_info.bootup_engine_clock;
  814. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  815. PPSMC_MSG_SetSclkSoftMin,
  816. cz_get_sclk_level(hwmgr,
  817. cz_hwmgr->sclk_dpm.soft_min_clk,
  818. PPSMC_MSG_SetSclkSoftMin));
  819. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  820. PPSMC_MSG_SetSclkSoftMax,
  821. cz_get_sclk_level(hwmgr,
  822. cz_hwmgr->sclk_dpm.soft_max_clk,
  823. PPSMC_MSG_SetSclkSoftMax));
  824. return 0;
  825. }
  826. static int cz_tf_reset_acp_boot_level(struct pp_hwmgr *hwmgr, void *input,
  827. void *output, void *storage, int result)
  828. {
  829. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  830. cz_hwmgr->acp_boot_level = 0xff;
  831. return 0;
  832. }
  833. static bool cz_dpm_check_smu_features(struct pp_hwmgr *hwmgr,
  834. unsigned long check_feature)
  835. {
  836. int result;
  837. unsigned long features;
  838. result = smum_send_msg_to_smc_with_parameter(hwmgr->smumgr, PPSMC_MSG_GetFeatureStatus, 0);
  839. if (result == 0) {
  840. features = smum_get_argument(hwmgr->smumgr);
  841. if (features & check_feature)
  842. return true;
  843. }
  844. return result;
  845. }
  846. static int cz_tf_check_for_dpm_disabled(struct pp_hwmgr *hwmgr, void *input,
  847. void *output, void *storage, int result)
  848. {
  849. if (cz_dpm_check_smu_features(hwmgr, SMU_EnabledFeatureScoreboard_SclkDpmOn))
  850. return PP_Result_TableImmediateExit;
  851. return 0;
  852. }
  853. static int cz_tf_enable_didt(struct pp_hwmgr *hwmgr, void *input,
  854. void *output, void *storage, int result)
  855. {
  856. /* TO DO */
  857. return 0;
  858. }
  859. static int cz_tf_check_for_dpm_enabled(struct pp_hwmgr *hwmgr,
  860. void *input, void *output,
  861. void *storage, int result)
  862. {
  863. if (!cz_dpm_check_smu_features(hwmgr,
  864. SMU_EnabledFeatureScoreboard_SclkDpmOn))
  865. return PP_Result_TableImmediateExit;
  866. return 0;
  867. }
  868. static const struct phm_master_table_item cz_disable_dpm_list[] = {
  869. { .tableFunction = cz_tf_check_for_dpm_enabled },
  870. { },
  871. };
  872. static const struct phm_master_table_header cz_disable_dpm_master = {
  873. 0,
  874. PHM_MasterTableFlag_None,
  875. cz_disable_dpm_list
  876. };
  877. static const struct phm_master_table_item cz_enable_dpm_list[] = {
  878. { .tableFunction = cz_tf_check_for_dpm_disabled },
  879. { .tableFunction = cz_tf_program_voting_clients },
  880. { .tableFunction = cz_tf_start_dpm },
  881. { .tableFunction = cz_tf_program_bootup_state },
  882. { .tableFunction = cz_tf_enable_didt },
  883. { .tableFunction = cz_tf_reset_acp_boot_level },
  884. { },
  885. };
  886. static const struct phm_master_table_header cz_enable_dpm_master = {
  887. 0,
  888. PHM_MasterTableFlag_None,
  889. cz_enable_dpm_list
  890. };
  891. static int cz_apply_state_adjust_rules(struct pp_hwmgr *hwmgr,
  892. struct pp_power_state *prequest_ps,
  893. const struct pp_power_state *pcurrent_ps)
  894. {
  895. struct cz_power_state *cz_ps =
  896. cast_PhwCzPowerState(&prequest_ps->hardware);
  897. const struct cz_power_state *cz_current_ps =
  898. cast_const_PhwCzPowerState(&pcurrent_ps->hardware);
  899. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  900. struct PP_Clocks clocks = {0, 0, 0, 0};
  901. bool force_high;
  902. uint32_t num_of_active_displays = 0;
  903. struct cgs_display_info info = {0};
  904. cz_ps->evclk = hwmgr->vce_arbiter.evclk;
  905. cz_ps->ecclk = hwmgr->vce_arbiter.ecclk;
  906. cz_ps->need_dfs_bypass = true;
  907. cz_hwmgr->video_start = (hwmgr->uvd_arbiter.vclk != 0 || hwmgr->uvd_arbiter.dclk != 0 ||
  908. hwmgr->vce_arbiter.evclk != 0 || hwmgr->vce_arbiter.ecclk != 0);
  909. cz_hwmgr->battery_state = (PP_StateUILabel_Battery == prequest_ps->classification.ui_label);
  910. clocks.memoryClock = hwmgr->display_config.min_mem_set_clock != 0 ?
  911. hwmgr->display_config.min_mem_set_clock :
  912. cz_hwmgr->sys_info.nbp_memory_clock[1];
  913. cgs_get_active_displays_info(hwmgr->device, &info);
  914. num_of_active_displays = info.display_count;
  915. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_StablePState))
  916. clocks.memoryClock = hwmgr->dyn_state.max_clock_voltage_on_ac.mclk;
  917. if (clocks.memoryClock < hwmgr->gfx_arbiter.mclk)
  918. clocks.memoryClock = hwmgr->gfx_arbiter.mclk;
  919. force_high = (clocks.memoryClock > cz_hwmgr->sys_info.nbp_memory_clock[CZ_NUM_NBPMEMORYCLOCK - 1])
  920. || (num_of_active_displays >= 3);
  921. cz_ps->action = cz_current_ps->action;
  922. if (!force_high && (cz_ps->action == FORCE_HIGH))
  923. cz_ps->action = CANCEL_FORCE_HIGH;
  924. else if (force_high && (cz_ps->action != FORCE_HIGH))
  925. cz_ps->action = FORCE_HIGH;
  926. else
  927. cz_ps->action = DO_NOTHING;
  928. return 0;
  929. }
  930. static int cz_hwmgr_backend_init(struct pp_hwmgr *hwmgr)
  931. {
  932. int result = 0;
  933. struct cz_hwmgr *data;
  934. data = kzalloc(sizeof(struct cz_hwmgr), GFP_KERNEL);
  935. if (data == NULL)
  936. return -ENOMEM;
  937. hwmgr->backend = data;
  938. result = cz_initialize_dpm_defaults(hwmgr);
  939. if (result != 0) {
  940. pr_err("cz_initialize_dpm_defaults failed\n");
  941. return result;
  942. }
  943. result = cz_get_system_info_data(hwmgr);
  944. if (result != 0) {
  945. pr_err("cz_get_system_info_data failed\n");
  946. return result;
  947. }
  948. cz_construct_boot_state(hwmgr);
  949. result = phm_construct_table(hwmgr, &cz_setup_asic_master,
  950. &(hwmgr->setup_asic));
  951. if (result != 0) {
  952. pr_err("Fail to construct setup ASIC\n");
  953. return result;
  954. }
  955. result = phm_construct_table(hwmgr, &cz_power_down_asic_master,
  956. &(hwmgr->power_down_asic));
  957. if (result != 0) {
  958. pr_err("Fail to construct power down ASIC\n");
  959. return result;
  960. }
  961. result = phm_construct_table(hwmgr, &cz_disable_dpm_master,
  962. &(hwmgr->disable_dynamic_state_management));
  963. if (result != 0) {
  964. pr_err("Fail to disable_dynamic_state\n");
  965. return result;
  966. }
  967. result = phm_construct_table(hwmgr, &cz_enable_dpm_master,
  968. &(hwmgr->enable_dynamic_state_management));
  969. if (result != 0) {
  970. pr_err("Fail to enable_dynamic_state\n");
  971. return result;
  972. }
  973. result = phm_construct_table(hwmgr, &cz_set_power_state_master,
  974. &(hwmgr->set_power_state));
  975. if (result != 0) {
  976. pr_err("Fail to construct set_power_state\n");
  977. return result;
  978. }
  979. hwmgr->platform_descriptor.hardwareActivityPerformanceLevels = CZ_MAX_HARDWARE_POWERLEVELS;
  980. result = phm_construct_table(hwmgr, &cz_phm_enable_clock_power_gatings_master, &(hwmgr->enable_clock_power_gatings));
  981. if (result != 0) {
  982. pr_err("Fail to construct enable_clock_power_gatings\n");
  983. return result;
  984. }
  985. return result;
  986. }
  987. static int cz_hwmgr_backend_fini(struct pp_hwmgr *hwmgr)
  988. {
  989. if (hwmgr != NULL) {
  990. phm_destroy_table(hwmgr, &(hwmgr->enable_clock_power_gatings));
  991. phm_destroy_table(hwmgr, &(hwmgr->set_power_state));
  992. phm_destroy_table(hwmgr, &(hwmgr->enable_dynamic_state_management));
  993. phm_destroy_table(hwmgr, &(hwmgr->disable_dynamic_state_management));
  994. phm_destroy_table(hwmgr, &(hwmgr->power_down_asic));
  995. phm_destroy_table(hwmgr, &(hwmgr->setup_asic));
  996. kfree(hwmgr->backend);
  997. hwmgr->backend = NULL;
  998. }
  999. return 0;
  1000. }
  1001. static int cz_phm_force_dpm_highest(struct pp_hwmgr *hwmgr)
  1002. {
  1003. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1004. if (cz_hwmgr->sclk_dpm.soft_min_clk !=
  1005. cz_hwmgr->sclk_dpm.soft_max_clk)
  1006. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1007. PPSMC_MSG_SetSclkSoftMin,
  1008. cz_get_sclk_level(hwmgr,
  1009. cz_hwmgr->sclk_dpm.soft_max_clk,
  1010. PPSMC_MSG_SetSclkSoftMin));
  1011. return 0;
  1012. }
  1013. static int cz_phm_unforce_dpm_levels(struct pp_hwmgr *hwmgr)
  1014. {
  1015. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1016. struct phm_clock_voltage_dependency_table *table =
  1017. hwmgr->dyn_state.vddc_dependency_on_sclk;
  1018. unsigned long clock = 0, level;
  1019. if (NULL == table || table->count <= 0)
  1020. return -EINVAL;
  1021. cz_hwmgr->sclk_dpm.soft_min_clk = table->entries[0].clk;
  1022. cz_hwmgr->sclk_dpm.hard_min_clk = table->entries[0].clk;
  1023. level = cz_get_max_sclk_level(hwmgr) - 1;
  1024. if (level < table->count)
  1025. clock = table->entries[level].clk;
  1026. else
  1027. clock = table->entries[table->count - 1].clk;
  1028. cz_hwmgr->sclk_dpm.soft_max_clk = clock;
  1029. cz_hwmgr->sclk_dpm.hard_max_clk = clock;
  1030. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1031. PPSMC_MSG_SetSclkSoftMin,
  1032. cz_get_sclk_level(hwmgr,
  1033. cz_hwmgr->sclk_dpm.soft_min_clk,
  1034. PPSMC_MSG_SetSclkSoftMin));
  1035. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1036. PPSMC_MSG_SetSclkSoftMax,
  1037. cz_get_sclk_level(hwmgr,
  1038. cz_hwmgr->sclk_dpm.soft_max_clk,
  1039. PPSMC_MSG_SetSclkSoftMax));
  1040. return 0;
  1041. }
  1042. static int cz_phm_force_dpm_lowest(struct pp_hwmgr *hwmgr)
  1043. {
  1044. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1045. if (cz_hwmgr->sclk_dpm.soft_min_clk !=
  1046. cz_hwmgr->sclk_dpm.soft_max_clk) {
  1047. cz_hwmgr->sclk_dpm.soft_max_clk =
  1048. cz_hwmgr->sclk_dpm.soft_min_clk;
  1049. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1050. PPSMC_MSG_SetSclkSoftMax,
  1051. cz_get_sclk_level(hwmgr,
  1052. cz_hwmgr->sclk_dpm.soft_max_clk,
  1053. PPSMC_MSG_SetSclkSoftMax));
  1054. }
  1055. return 0;
  1056. }
  1057. static int cz_dpm_force_dpm_level(struct pp_hwmgr *hwmgr,
  1058. enum amd_dpm_forced_level level)
  1059. {
  1060. int ret = 0;
  1061. switch (level) {
  1062. case AMD_DPM_FORCED_LEVEL_HIGH:
  1063. ret = cz_phm_force_dpm_highest(hwmgr);
  1064. if (ret)
  1065. return ret;
  1066. break;
  1067. case AMD_DPM_FORCED_LEVEL_LOW:
  1068. ret = cz_phm_force_dpm_lowest(hwmgr);
  1069. if (ret)
  1070. return ret;
  1071. break;
  1072. case AMD_DPM_FORCED_LEVEL_AUTO:
  1073. ret = cz_phm_unforce_dpm_levels(hwmgr);
  1074. if (ret)
  1075. return ret;
  1076. break;
  1077. default:
  1078. break;
  1079. }
  1080. hwmgr->dpm_level = level;
  1081. return ret;
  1082. }
  1083. int cz_dpm_powerdown_uvd(struct pp_hwmgr *hwmgr)
  1084. {
  1085. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1086. PHM_PlatformCaps_UVDPowerGating))
  1087. return smum_send_msg_to_smc(hwmgr->smumgr,
  1088. PPSMC_MSG_UVDPowerOFF);
  1089. return 0;
  1090. }
  1091. int cz_dpm_powerup_uvd(struct pp_hwmgr *hwmgr)
  1092. {
  1093. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1094. PHM_PlatformCaps_UVDPowerGating)) {
  1095. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1096. PHM_PlatformCaps_UVDDynamicPowerGating)) {
  1097. return smum_send_msg_to_smc_with_parameter(
  1098. hwmgr->smumgr,
  1099. PPSMC_MSG_UVDPowerON, 1);
  1100. } else {
  1101. return smum_send_msg_to_smc_with_parameter(
  1102. hwmgr->smumgr,
  1103. PPSMC_MSG_UVDPowerON, 0);
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. int cz_dpm_update_uvd_dpm(struct pp_hwmgr *hwmgr, bool bgate)
  1109. {
  1110. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1111. struct phm_uvd_clock_voltage_dependency_table *ptable =
  1112. hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
  1113. if (!bgate) {
  1114. /* Stable Pstate is enabled and we need to set the UVD DPM to highest level */
  1115. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1116. PHM_PlatformCaps_StablePState)) {
  1117. cz_hwmgr->uvd_dpm.hard_min_clk =
  1118. ptable->entries[ptable->count - 1].vclk;
  1119. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1120. PPSMC_MSG_SetUvdHardMin,
  1121. cz_get_uvd_level(hwmgr,
  1122. cz_hwmgr->uvd_dpm.hard_min_clk,
  1123. PPSMC_MSG_SetUvdHardMin));
  1124. cz_enable_disable_uvd_dpm(hwmgr, true);
  1125. } else {
  1126. cz_enable_disable_uvd_dpm(hwmgr, true);
  1127. }
  1128. } else {
  1129. cz_enable_disable_uvd_dpm(hwmgr, false);
  1130. }
  1131. return 0;
  1132. }
  1133. int cz_dpm_update_vce_dpm(struct pp_hwmgr *hwmgr)
  1134. {
  1135. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1136. struct phm_vce_clock_voltage_dependency_table *ptable =
  1137. hwmgr->dyn_state.vce_clock_voltage_dependency_table;
  1138. /* Stable Pstate is enabled and we need to set the VCE DPM to highest level */
  1139. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1140. PHM_PlatformCaps_StablePState)) {
  1141. cz_hwmgr->vce_dpm.hard_min_clk =
  1142. ptable->entries[ptable->count - 1].ecclk;
  1143. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1144. PPSMC_MSG_SetEclkHardMin,
  1145. cz_get_eclk_level(hwmgr,
  1146. cz_hwmgr->vce_dpm.hard_min_clk,
  1147. PPSMC_MSG_SetEclkHardMin));
  1148. } else {
  1149. /*Program HardMin based on the vce_arbiter.ecclk */
  1150. if (hwmgr->vce_arbiter.ecclk == 0) {
  1151. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1152. PPSMC_MSG_SetEclkHardMin, 0);
  1153. /* disable ECLK DPM 0. Otherwise VCE could hang if
  1154. * switching SCLK from DPM 0 to 6/7 */
  1155. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1156. PPSMC_MSG_SetEclkSoftMin, 1);
  1157. } else {
  1158. cz_hwmgr->vce_dpm.hard_min_clk = hwmgr->vce_arbiter.ecclk;
  1159. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1160. PPSMC_MSG_SetEclkHardMin,
  1161. cz_get_eclk_level(hwmgr,
  1162. cz_hwmgr->vce_dpm.hard_min_clk,
  1163. PPSMC_MSG_SetEclkHardMin));
  1164. }
  1165. }
  1166. return 0;
  1167. }
  1168. int cz_dpm_powerdown_vce(struct pp_hwmgr *hwmgr)
  1169. {
  1170. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1171. PHM_PlatformCaps_VCEPowerGating))
  1172. return smum_send_msg_to_smc(hwmgr->smumgr,
  1173. PPSMC_MSG_VCEPowerOFF);
  1174. return 0;
  1175. }
  1176. int cz_dpm_powerup_vce(struct pp_hwmgr *hwmgr)
  1177. {
  1178. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1179. PHM_PlatformCaps_VCEPowerGating))
  1180. return smum_send_msg_to_smc(hwmgr->smumgr,
  1181. PPSMC_MSG_VCEPowerON);
  1182. return 0;
  1183. }
  1184. static int cz_dpm_get_mclk(struct pp_hwmgr *hwmgr, bool low)
  1185. {
  1186. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1187. return cz_hwmgr->sys_info.bootup_uma_clock;
  1188. }
  1189. static int cz_dpm_get_sclk(struct pp_hwmgr *hwmgr, bool low)
  1190. {
  1191. struct pp_power_state *ps;
  1192. struct cz_power_state *cz_ps;
  1193. if (hwmgr == NULL)
  1194. return -EINVAL;
  1195. ps = hwmgr->request_ps;
  1196. if (ps == NULL)
  1197. return -EINVAL;
  1198. cz_ps = cast_PhwCzPowerState(&ps->hardware);
  1199. if (low)
  1200. return cz_ps->levels[0].engineClock;
  1201. else
  1202. return cz_ps->levels[cz_ps->level-1].engineClock;
  1203. }
  1204. static int cz_dpm_patch_boot_state(struct pp_hwmgr *hwmgr,
  1205. struct pp_hw_power_state *hw_ps)
  1206. {
  1207. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1208. struct cz_power_state *cz_ps = cast_PhwCzPowerState(hw_ps);
  1209. cz_ps->level = 1;
  1210. cz_ps->nbps_flags = 0;
  1211. cz_ps->bapm_flags = 0;
  1212. cz_ps->levels[0] = cz_hwmgr->boot_power_level;
  1213. return 0;
  1214. }
  1215. static int cz_dpm_get_pp_table_entry_callback(
  1216. struct pp_hwmgr *hwmgr,
  1217. struct pp_hw_power_state *hw_ps,
  1218. unsigned int index,
  1219. const void *clock_info)
  1220. {
  1221. struct cz_power_state *cz_ps = cast_PhwCzPowerState(hw_ps);
  1222. const ATOM_PPLIB_CZ_CLOCK_INFO *cz_clock_info = clock_info;
  1223. struct phm_clock_voltage_dependency_table *table =
  1224. hwmgr->dyn_state.vddc_dependency_on_sclk;
  1225. uint8_t clock_info_index = cz_clock_info->index;
  1226. if (clock_info_index > (uint8_t)(hwmgr->platform_descriptor.hardwareActivityPerformanceLevels - 1))
  1227. clock_info_index = (uint8_t)(hwmgr->platform_descriptor.hardwareActivityPerformanceLevels - 1);
  1228. cz_ps->levels[index].engineClock = table->entries[clock_info_index].clk;
  1229. cz_ps->levels[index].vddcIndex = (uint8_t)table->entries[clock_info_index].v;
  1230. cz_ps->level = index + 1;
  1231. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_SclkDeepSleep)) {
  1232. cz_ps->levels[index].dsDividerIndex = 5;
  1233. cz_ps->levels[index].ssDividerIndex = 5;
  1234. }
  1235. return 0;
  1236. }
  1237. static int cz_dpm_get_num_of_pp_table_entries(struct pp_hwmgr *hwmgr)
  1238. {
  1239. int result;
  1240. unsigned long ret = 0;
  1241. result = pp_tables_get_num_of_entries(hwmgr, &ret);
  1242. return result ? 0 : ret;
  1243. }
  1244. static int cz_dpm_get_pp_table_entry(struct pp_hwmgr *hwmgr,
  1245. unsigned long entry, struct pp_power_state *ps)
  1246. {
  1247. int result;
  1248. struct cz_power_state *cz_ps;
  1249. ps->hardware.magic = PhwCz_Magic;
  1250. cz_ps = cast_PhwCzPowerState(&(ps->hardware));
  1251. result = pp_tables_get_entry(hwmgr, entry, ps,
  1252. cz_dpm_get_pp_table_entry_callback);
  1253. cz_ps->uvd_clocks.vclk = ps->uvd_clocks.VCLK;
  1254. cz_ps->uvd_clocks.dclk = ps->uvd_clocks.DCLK;
  1255. return result;
  1256. }
  1257. static int cz_get_power_state_size(struct pp_hwmgr *hwmgr)
  1258. {
  1259. return sizeof(struct cz_power_state);
  1260. }
  1261. static void cz_hw_print_display_cfg(
  1262. const struct cc6_settings *cc6_settings)
  1263. {
  1264. PP_DBG_LOG("New Display Configuration:\n");
  1265. PP_DBG_LOG(" cpu_cc6_disable: %d\n",
  1266. cc6_settings->cpu_cc6_disable);
  1267. PP_DBG_LOG(" cpu_pstate_disable: %d\n",
  1268. cc6_settings->cpu_pstate_disable);
  1269. PP_DBG_LOG(" nb_pstate_switch_disable: %d\n",
  1270. cc6_settings->nb_pstate_switch_disable);
  1271. PP_DBG_LOG(" cpu_pstate_separation_time: %d\n\n",
  1272. cc6_settings->cpu_pstate_separation_time);
  1273. }
  1274. static int cz_set_cpu_power_state(struct pp_hwmgr *hwmgr)
  1275. {
  1276. struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
  1277. uint32_t data = 0;
  1278. if (hw_data->cc6_settings.cc6_setting_changed) {
  1279. hw_data->cc6_settings.cc6_setting_changed = false;
  1280. cz_hw_print_display_cfg(&hw_data->cc6_settings);
  1281. data |= (hw_data->cc6_settings.cpu_pstate_separation_time
  1282. & PWRMGT_SEPARATION_TIME_MASK)
  1283. << PWRMGT_SEPARATION_TIME_SHIFT;
  1284. data |= (hw_data->cc6_settings.cpu_cc6_disable ? 0x1 : 0x0)
  1285. << PWRMGT_DISABLE_CPU_CSTATES_SHIFT;
  1286. data |= (hw_data->cc6_settings.cpu_pstate_disable ? 0x1 : 0x0)
  1287. << PWRMGT_DISABLE_CPU_PSTATES_SHIFT;
  1288. PP_DBG_LOG("SetDisplaySizePowerParams data: 0x%X\n",
  1289. data);
  1290. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1291. PPSMC_MSG_SetDisplaySizePowerParams,
  1292. data);
  1293. }
  1294. return 0;
  1295. }
  1296. static int cz_store_cc6_data(struct pp_hwmgr *hwmgr, uint32_t separation_time,
  1297. bool cc6_disable, bool pstate_disable, bool pstate_switch_disable)
  1298. {
  1299. struct cz_hwmgr *hw_data = (struct cz_hwmgr *)(hwmgr->backend);
  1300. if (separation_time !=
  1301. hw_data->cc6_settings.cpu_pstate_separation_time ||
  1302. cc6_disable != hw_data->cc6_settings.cpu_cc6_disable ||
  1303. pstate_disable != hw_data->cc6_settings.cpu_pstate_disable ||
  1304. pstate_switch_disable != hw_data->cc6_settings.nb_pstate_switch_disable) {
  1305. hw_data->cc6_settings.cc6_setting_changed = true;
  1306. hw_data->cc6_settings.cpu_pstate_separation_time =
  1307. separation_time;
  1308. hw_data->cc6_settings.cpu_cc6_disable =
  1309. cc6_disable;
  1310. hw_data->cc6_settings.cpu_pstate_disable =
  1311. pstate_disable;
  1312. hw_data->cc6_settings.nb_pstate_switch_disable =
  1313. pstate_switch_disable;
  1314. }
  1315. return 0;
  1316. }
  1317. static int cz_get_dal_power_level(struct pp_hwmgr *hwmgr,
  1318. struct amd_pp_simple_clock_info *info)
  1319. {
  1320. uint32_t i;
  1321. const struct phm_clock_voltage_dependency_table *table =
  1322. hwmgr->dyn_state.vddc_dep_on_dal_pwrl;
  1323. const struct phm_clock_and_voltage_limits *limits =
  1324. &hwmgr->dyn_state.max_clock_voltage_on_ac;
  1325. info->engine_max_clock = limits->sclk;
  1326. info->memory_max_clock = limits->mclk;
  1327. for (i = table->count - 1; i > 0; i--) {
  1328. if (limits->vddc >= table->entries[i].v) {
  1329. info->level = table->entries[i].clk;
  1330. return 0;
  1331. }
  1332. }
  1333. return -EINVAL;
  1334. }
  1335. static int cz_force_clock_level(struct pp_hwmgr *hwmgr,
  1336. enum pp_clock_type type, uint32_t mask)
  1337. {
  1338. if (hwmgr->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
  1339. return -EINVAL;
  1340. switch (type) {
  1341. case PP_SCLK:
  1342. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1343. PPSMC_MSG_SetSclkSoftMin,
  1344. mask);
  1345. smum_send_msg_to_smc_with_parameter(hwmgr->smumgr,
  1346. PPSMC_MSG_SetSclkSoftMax,
  1347. mask);
  1348. break;
  1349. default:
  1350. break;
  1351. }
  1352. return 0;
  1353. }
  1354. static int cz_print_clock_levels(struct pp_hwmgr *hwmgr,
  1355. enum pp_clock_type type, char *buf)
  1356. {
  1357. struct phm_clock_voltage_dependency_table *sclk_table =
  1358. hwmgr->dyn_state.vddc_dependency_on_sclk;
  1359. int i, now, size = 0;
  1360. switch (type) {
  1361. case PP_SCLK:
  1362. now = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device,
  1363. CGS_IND_REG__SMC,
  1364. ixTARGET_AND_CURRENT_PROFILE_INDEX),
  1365. TARGET_AND_CURRENT_PROFILE_INDEX,
  1366. CURR_SCLK_INDEX);
  1367. for (i = 0; i < sclk_table->count; i++)
  1368. size += sprintf(buf + size, "%d: %uMhz %s\n",
  1369. i, sclk_table->entries[i].clk / 100,
  1370. (i == now) ? "*" : "");
  1371. break;
  1372. default:
  1373. break;
  1374. }
  1375. return size;
  1376. }
  1377. static int cz_get_performance_level(struct pp_hwmgr *hwmgr, const struct pp_hw_power_state *state,
  1378. PHM_PerformanceLevelDesignation designation, uint32_t index,
  1379. PHM_PerformanceLevel *level)
  1380. {
  1381. const struct cz_power_state *ps;
  1382. struct cz_hwmgr *data;
  1383. uint32_t level_index;
  1384. uint32_t i;
  1385. if (level == NULL || hwmgr == NULL || state == NULL)
  1386. return -EINVAL;
  1387. data = (struct cz_hwmgr *)(hwmgr->backend);
  1388. ps = cast_const_PhwCzPowerState(state);
  1389. level_index = index > ps->level - 1 ? ps->level - 1 : index;
  1390. level->coreClock = ps->levels[level_index].engineClock;
  1391. if (designation == PHM_PerformanceLevelDesignation_PowerContainment) {
  1392. for (i = 1; i < ps->level; i++) {
  1393. if (ps->levels[i].engineClock > data->dce_slow_sclk_threshold) {
  1394. level->coreClock = ps->levels[i].engineClock;
  1395. break;
  1396. }
  1397. }
  1398. }
  1399. if (level_index == 0)
  1400. level->memory_clock = data->sys_info.nbp_memory_clock[CZ_NUM_NBPMEMORYCLOCK - 1];
  1401. else
  1402. level->memory_clock = data->sys_info.nbp_memory_clock[0];
  1403. level->vddc = (cz_convert_8Bit_index_to_voltage(hwmgr, ps->levels[level_index].vddcIndex) + 2) / 4;
  1404. level->nonLocalMemoryFreq = 0;
  1405. level->nonLocalMemoryWidth = 0;
  1406. return 0;
  1407. }
  1408. static int cz_get_current_shallow_sleep_clocks(struct pp_hwmgr *hwmgr,
  1409. const struct pp_hw_power_state *state, struct pp_clock_info *clock_info)
  1410. {
  1411. const struct cz_power_state *ps = cast_const_PhwCzPowerState(state);
  1412. clock_info->min_eng_clk = ps->levels[0].engineClock / (1 << (ps->levels[0].ssDividerIndex));
  1413. clock_info->max_eng_clk = ps->levels[ps->level - 1].engineClock / (1 << (ps->levels[ps->level - 1].ssDividerIndex));
  1414. return 0;
  1415. }
  1416. static int cz_get_clock_by_type(struct pp_hwmgr *hwmgr, enum amd_pp_clock_type type,
  1417. struct amd_pp_clocks *clocks)
  1418. {
  1419. struct cz_hwmgr *data = (struct cz_hwmgr *)(hwmgr->backend);
  1420. int i;
  1421. struct phm_clock_voltage_dependency_table *table;
  1422. clocks->count = cz_get_max_sclk_level(hwmgr);
  1423. switch (type) {
  1424. case amd_pp_disp_clock:
  1425. for (i = 0; i < clocks->count; i++)
  1426. clocks->clock[i] = data->sys_info.display_clock[i];
  1427. break;
  1428. case amd_pp_sys_clock:
  1429. table = hwmgr->dyn_state.vddc_dependency_on_sclk;
  1430. for (i = 0; i < clocks->count; i++)
  1431. clocks->clock[i] = table->entries[i].clk;
  1432. break;
  1433. case amd_pp_mem_clock:
  1434. clocks->count = CZ_NUM_NBPMEMORYCLOCK;
  1435. for (i = 0; i < clocks->count; i++)
  1436. clocks->clock[i] = data->sys_info.nbp_memory_clock[clocks->count - 1 - i];
  1437. break;
  1438. default:
  1439. return -1;
  1440. }
  1441. return 0;
  1442. }
  1443. static int cz_get_max_high_clocks(struct pp_hwmgr *hwmgr, struct amd_pp_simple_clock_info *clocks)
  1444. {
  1445. struct phm_clock_voltage_dependency_table *table =
  1446. hwmgr->dyn_state.vddc_dependency_on_sclk;
  1447. unsigned long level;
  1448. const struct phm_clock_and_voltage_limits *limits =
  1449. &hwmgr->dyn_state.max_clock_voltage_on_ac;
  1450. if ((NULL == table) || (table->count <= 0) || (clocks == NULL))
  1451. return -EINVAL;
  1452. level = cz_get_max_sclk_level(hwmgr) - 1;
  1453. if (level < table->count)
  1454. clocks->engine_max_clock = table->entries[level].clk;
  1455. else
  1456. clocks->engine_max_clock = table->entries[table->count - 1].clk;
  1457. clocks->memory_max_clock = limits->mclk;
  1458. return 0;
  1459. }
  1460. static int cz_thermal_get_temperature(struct pp_hwmgr *hwmgr)
  1461. {
  1462. int actual_temp = 0;
  1463. uint32_t val = cgs_read_ind_register(hwmgr->device,
  1464. CGS_IND_REG__SMC, ixTHM_TCON_CUR_TMP);
  1465. uint32_t temp = PHM_GET_FIELD(val, THM_TCON_CUR_TMP, CUR_TEMP);
  1466. if (PHM_GET_FIELD(val, THM_TCON_CUR_TMP, CUR_TEMP_RANGE_SEL))
  1467. actual_temp = ((temp / 8) - 49) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
  1468. else
  1469. actual_temp = (temp / 8) * PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
  1470. return actual_temp;
  1471. }
  1472. static int cz_read_sensor(struct pp_hwmgr *hwmgr, int idx,
  1473. void *value, int *size)
  1474. {
  1475. struct cz_hwmgr *cz_hwmgr = (struct cz_hwmgr *)(hwmgr->backend);
  1476. struct phm_clock_voltage_dependency_table *table =
  1477. hwmgr->dyn_state.vddc_dependency_on_sclk;
  1478. struct phm_vce_clock_voltage_dependency_table *vce_table =
  1479. hwmgr->dyn_state.vce_clock_voltage_dependency_table;
  1480. struct phm_uvd_clock_voltage_dependency_table *uvd_table =
  1481. hwmgr->dyn_state.uvd_clock_voltage_dependency_table;
  1482. uint32_t sclk_index = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixTARGET_AND_CURRENT_PROFILE_INDEX),
  1483. TARGET_AND_CURRENT_PROFILE_INDEX, CURR_SCLK_INDEX);
  1484. uint32_t uvd_index = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
  1485. TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_UVD_INDEX);
  1486. uint32_t vce_index = PHM_GET_FIELD(cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
  1487. TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_VCE_INDEX);
  1488. uint32_t sclk, vclk, dclk, ecclk, tmp, activity_percent;
  1489. uint16_t vddnb, vddgfx;
  1490. int result;
  1491. /* size must be at least 4 bytes for all sensors */
  1492. if (*size < 4)
  1493. return -EINVAL;
  1494. *size = 4;
  1495. switch (idx) {
  1496. case AMDGPU_PP_SENSOR_GFX_SCLK:
  1497. if (sclk_index < NUM_SCLK_LEVELS) {
  1498. sclk = table->entries[sclk_index].clk;
  1499. *((uint32_t *)value) = sclk;
  1500. return 0;
  1501. }
  1502. return -EINVAL;
  1503. case AMDGPU_PP_SENSOR_VDDNB:
  1504. tmp = (cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixSMUSVI_NB_CURRENTVID) &
  1505. CURRENT_NB_VID_MASK) >> CURRENT_NB_VID__SHIFT;
  1506. vddnb = cz_convert_8Bit_index_to_voltage(hwmgr, tmp);
  1507. *((uint32_t *)value) = vddnb;
  1508. return 0;
  1509. case AMDGPU_PP_SENSOR_VDDGFX:
  1510. tmp = (cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC, ixSMUSVI_GFX_CURRENTVID) &
  1511. CURRENT_GFX_VID_MASK) >> CURRENT_GFX_VID__SHIFT;
  1512. vddgfx = cz_convert_8Bit_index_to_voltage(hwmgr, (u16)tmp);
  1513. *((uint32_t *)value) = vddgfx;
  1514. return 0;
  1515. case AMDGPU_PP_SENSOR_UVD_VCLK:
  1516. if (!cz_hwmgr->uvd_power_gated) {
  1517. if (uvd_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
  1518. return -EINVAL;
  1519. } else {
  1520. vclk = uvd_table->entries[uvd_index].vclk;
  1521. *((uint32_t *)value) = vclk;
  1522. return 0;
  1523. }
  1524. }
  1525. *((uint32_t *)value) = 0;
  1526. return 0;
  1527. case AMDGPU_PP_SENSOR_UVD_DCLK:
  1528. if (!cz_hwmgr->uvd_power_gated) {
  1529. if (uvd_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
  1530. return -EINVAL;
  1531. } else {
  1532. dclk = uvd_table->entries[uvd_index].dclk;
  1533. *((uint32_t *)value) = dclk;
  1534. return 0;
  1535. }
  1536. }
  1537. *((uint32_t *)value) = 0;
  1538. return 0;
  1539. case AMDGPU_PP_SENSOR_VCE_ECCLK:
  1540. if (!cz_hwmgr->vce_power_gated) {
  1541. if (vce_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
  1542. return -EINVAL;
  1543. } else {
  1544. ecclk = vce_table->entries[vce_index].ecclk;
  1545. *((uint32_t *)value) = ecclk;
  1546. return 0;
  1547. }
  1548. }
  1549. *((uint32_t *)value) = 0;
  1550. return 0;
  1551. case AMDGPU_PP_SENSOR_GPU_LOAD:
  1552. result = smum_send_msg_to_smc(hwmgr->smumgr, PPSMC_MSG_GetAverageGraphicsActivity);
  1553. if (0 == result) {
  1554. activity_percent = cgs_read_register(hwmgr->device, mmSMU_MP1_SRBM2P_ARG_0);
  1555. activity_percent = activity_percent > 100 ? 100 : activity_percent;
  1556. } else {
  1557. activity_percent = 50;
  1558. }
  1559. *((uint32_t *)value) = activity_percent;
  1560. return 0;
  1561. case AMDGPU_PP_SENSOR_UVD_POWER:
  1562. *((uint32_t *)value) = cz_hwmgr->uvd_power_gated ? 0 : 1;
  1563. return 0;
  1564. case AMDGPU_PP_SENSOR_VCE_POWER:
  1565. *((uint32_t *)value) = cz_hwmgr->vce_power_gated ? 0 : 1;
  1566. return 0;
  1567. case AMDGPU_PP_SENSOR_GPU_TEMP:
  1568. *((uint32_t *)value) = cz_thermal_get_temperature(hwmgr);
  1569. return 0;
  1570. default:
  1571. return -EINVAL;
  1572. }
  1573. }
  1574. static const struct pp_hwmgr_func cz_hwmgr_funcs = {
  1575. .backend_init = cz_hwmgr_backend_init,
  1576. .backend_fini = cz_hwmgr_backend_fini,
  1577. .asic_setup = NULL,
  1578. .apply_state_adjust_rules = cz_apply_state_adjust_rules,
  1579. .force_dpm_level = cz_dpm_force_dpm_level,
  1580. .get_power_state_size = cz_get_power_state_size,
  1581. .powerdown_uvd = cz_dpm_powerdown_uvd,
  1582. .powergate_uvd = cz_dpm_powergate_uvd,
  1583. .powergate_vce = cz_dpm_powergate_vce,
  1584. .get_mclk = cz_dpm_get_mclk,
  1585. .get_sclk = cz_dpm_get_sclk,
  1586. .patch_boot_state = cz_dpm_patch_boot_state,
  1587. .get_pp_table_entry = cz_dpm_get_pp_table_entry,
  1588. .get_num_of_pp_table_entries = cz_dpm_get_num_of_pp_table_entries,
  1589. .set_cpu_power_state = cz_set_cpu_power_state,
  1590. .store_cc6_data = cz_store_cc6_data,
  1591. .force_clock_level = cz_force_clock_level,
  1592. .print_clock_levels = cz_print_clock_levels,
  1593. .get_dal_power_level = cz_get_dal_power_level,
  1594. .get_performance_level = cz_get_performance_level,
  1595. .get_current_shallow_sleep_clocks = cz_get_current_shallow_sleep_clocks,
  1596. .get_clock_by_type = cz_get_clock_by_type,
  1597. .get_max_high_clocks = cz_get_max_high_clocks,
  1598. .read_sensor = cz_read_sensor,
  1599. };
  1600. int cz_init_function_pointers(struct pp_hwmgr *hwmgr)
  1601. {
  1602. hwmgr->hwmgr_func = &cz_hwmgr_funcs;
  1603. hwmgr->pptable_func = &pptable_funcs;
  1604. return 0;
  1605. }