cz_dpm.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  1. /*
  2. * Copyright 2014 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 <linux/firmware.h>
  24. #include <linux/seq_file.h>
  25. #include "drmP.h"
  26. #include "amdgpu.h"
  27. #include "amdgpu_pm.h"
  28. #include "amdgpu_atombios.h"
  29. #include "vid.h"
  30. #include "vi_dpm.h"
  31. #include "amdgpu_dpm.h"
  32. #include "cz_dpm.h"
  33. #include "cz_ppsmc.h"
  34. #include "atom.h"
  35. #include "smu/smu_8_0_d.h"
  36. #include "smu/smu_8_0_sh_mask.h"
  37. #include "gca/gfx_8_0_d.h"
  38. #include "gca/gfx_8_0_sh_mask.h"
  39. #include "gmc/gmc_8_1_d.h"
  40. #include "bif/bif_5_1_d.h"
  41. #include "gfx_v8_0.h"
  42. static void cz_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate);
  43. static void cz_dpm_powergate_vce(struct amdgpu_device *adev, bool gate);
  44. static struct cz_ps *cz_get_ps(struct amdgpu_ps *rps)
  45. {
  46. struct cz_ps *ps = rps->ps_priv;
  47. return ps;
  48. }
  49. static struct cz_power_info *cz_get_pi(struct amdgpu_device *adev)
  50. {
  51. struct cz_power_info *pi = adev->pm.dpm.priv;
  52. return pi;
  53. }
  54. static uint16_t cz_convert_8bit_index_to_voltage(struct amdgpu_device *adev,
  55. uint16_t voltage)
  56. {
  57. uint16_t tmp = 6200 - voltage * 25;
  58. return tmp;
  59. }
  60. static void cz_construct_max_power_limits_table(struct amdgpu_device *adev,
  61. struct amdgpu_clock_and_voltage_limits *table)
  62. {
  63. struct cz_power_info *pi = cz_get_pi(adev);
  64. struct amdgpu_clock_voltage_dependency_table *dep_table =
  65. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  66. if (dep_table->count > 0) {
  67. table->sclk = dep_table->entries[dep_table->count - 1].clk;
  68. table->vddc = cz_convert_8bit_index_to_voltage(adev,
  69. dep_table->entries[dep_table->count - 1].v);
  70. }
  71. table->mclk = pi->sys_info.nbp_memory_clock[0];
  72. }
  73. union igp_info {
  74. struct _ATOM_INTEGRATED_SYSTEM_INFO info;
  75. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
  76. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
  77. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
  78. };
  79. static int cz_parse_sys_info_table(struct amdgpu_device *adev)
  80. {
  81. struct cz_power_info *pi = cz_get_pi(adev);
  82. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  83. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  84. union igp_info *igp_info;
  85. u8 frev, crev;
  86. u16 data_offset;
  87. int i = 0;
  88. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  89. &frev, &crev, &data_offset)) {
  90. igp_info = (union igp_info *)(mode_info->atom_context->bios +
  91. data_offset);
  92. if (crev != 9) {
  93. DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
  94. return -EINVAL;
  95. }
  96. pi->sys_info.bootup_sclk =
  97. le32_to_cpu(igp_info->info_9.ulBootUpEngineClock);
  98. pi->sys_info.bootup_uma_clk =
  99. le32_to_cpu(igp_info->info_9.ulBootUpUMAClock);
  100. pi->sys_info.dentist_vco_freq =
  101. le32_to_cpu(igp_info->info_9.ulDentistVCOFreq);
  102. pi->sys_info.bootup_nb_voltage_index =
  103. le16_to_cpu(igp_info->info_9.usBootUpNBVoltage);
  104. if (igp_info->info_9.ucHtcTmpLmt == 0)
  105. pi->sys_info.htc_tmp_lmt = 203;
  106. else
  107. pi->sys_info.htc_tmp_lmt = igp_info->info_9.ucHtcTmpLmt;
  108. if (igp_info->info_9.ucHtcHystLmt == 0)
  109. pi->sys_info.htc_hyst_lmt = 5;
  110. else
  111. pi->sys_info.htc_hyst_lmt = igp_info->info_9.ucHtcHystLmt;
  112. if (pi->sys_info.htc_tmp_lmt <= pi->sys_info.htc_hyst_lmt) {
  113. DRM_ERROR("The htcTmpLmt should be larger than htcHystLmt.\n");
  114. return -EINVAL;
  115. }
  116. if (le32_to_cpu(igp_info->info_9.ulSystemConfig) & (1 << 3) &&
  117. pi->enable_nb_ps_policy)
  118. pi->sys_info.nb_dpm_enable = true;
  119. else
  120. pi->sys_info.nb_dpm_enable = false;
  121. for (i = 0; i < CZ_NUM_NBPSTATES; i++) {
  122. if (i < CZ_NUM_NBPMEMORY_CLOCK)
  123. pi->sys_info.nbp_memory_clock[i] =
  124. le32_to_cpu(igp_info->info_9.ulNbpStateMemclkFreq[i]);
  125. pi->sys_info.nbp_n_clock[i] =
  126. le32_to_cpu(igp_info->info_9.ulNbpStateNClkFreq[i]);
  127. }
  128. for (i = 0; i < CZ_MAX_DISPLAY_CLOCK_LEVEL; i++)
  129. pi->sys_info.display_clock[i] =
  130. le32_to_cpu(igp_info->info_9.sDispClkVoltageMapping[i].ulMaximumSupportedCLK);
  131. for (i = 0; i < CZ_NUM_NBPSTATES; i++)
  132. pi->sys_info.nbp_voltage_index[i] =
  133. le32_to_cpu(igp_info->info_9.usNBPStateVoltage[i]);
  134. if (le32_to_cpu(igp_info->info_9.ulGPUCapInfo) &
  135. SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS)
  136. pi->caps_enable_dfs_bypass = true;
  137. pi->sys_info.uma_channel_number =
  138. igp_info->info_9.ucUMAChannelNumber;
  139. cz_construct_max_power_limits_table(adev,
  140. &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
  141. }
  142. return 0;
  143. }
  144. static void cz_patch_voltage_values(struct amdgpu_device *adev)
  145. {
  146. int i;
  147. struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
  148. &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
  149. struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
  150. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  151. struct amdgpu_clock_voltage_dependency_table *acp_table =
  152. &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
  153. if (uvd_table->count) {
  154. for (i = 0; i < uvd_table->count; i++)
  155. uvd_table->entries[i].v =
  156. cz_convert_8bit_index_to_voltage(adev,
  157. uvd_table->entries[i].v);
  158. }
  159. if (vce_table->count) {
  160. for (i = 0; i < vce_table->count; i++)
  161. vce_table->entries[i].v =
  162. cz_convert_8bit_index_to_voltage(adev,
  163. vce_table->entries[i].v);
  164. }
  165. if (acp_table->count) {
  166. for (i = 0; i < acp_table->count; i++)
  167. acp_table->entries[i].v =
  168. cz_convert_8bit_index_to_voltage(adev,
  169. acp_table->entries[i].v);
  170. }
  171. }
  172. static void cz_construct_boot_state(struct amdgpu_device *adev)
  173. {
  174. struct cz_power_info *pi = cz_get_pi(adev);
  175. pi->boot_pl.sclk = pi->sys_info.bootup_sclk;
  176. pi->boot_pl.vddc_index = pi->sys_info.bootup_nb_voltage_index;
  177. pi->boot_pl.ds_divider_index = 0;
  178. pi->boot_pl.ss_divider_index = 0;
  179. pi->boot_pl.allow_gnb_slow = 1;
  180. pi->boot_pl.force_nbp_state = 0;
  181. pi->boot_pl.display_wm = 0;
  182. pi->boot_pl.vce_wm = 0;
  183. }
  184. static void cz_patch_boot_state(struct amdgpu_device *adev,
  185. struct cz_ps *ps)
  186. {
  187. struct cz_power_info *pi = cz_get_pi(adev);
  188. ps->num_levels = 1;
  189. ps->levels[0] = pi->boot_pl;
  190. }
  191. union pplib_clock_info {
  192. struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
  193. struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
  194. struct _ATOM_PPLIB_CZ_CLOCK_INFO carrizo;
  195. };
  196. static void cz_parse_pplib_clock_info(struct amdgpu_device *adev,
  197. struct amdgpu_ps *rps, int index,
  198. union pplib_clock_info *clock_info)
  199. {
  200. struct cz_power_info *pi = cz_get_pi(adev);
  201. struct cz_ps *ps = cz_get_ps(rps);
  202. struct cz_pl *pl = &ps->levels[index];
  203. struct amdgpu_clock_voltage_dependency_table *table =
  204. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  205. pl->sclk = table->entries[clock_info->carrizo.index].clk;
  206. pl->vddc_index = table->entries[clock_info->carrizo.index].v;
  207. ps->num_levels = index + 1;
  208. if (pi->caps_sclk_ds) {
  209. pl->ds_divider_index = 5;
  210. pl->ss_divider_index = 5;
  211. }
  212. }
  213. static void cz_parse_pplib_non_clock_info(struct amdgpu_device *adev,
  214. struct amdgpu_ps *rps,
  215. struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
  216. u8 table_rev)
  217. {
  218. struct cz_ps *ps = cz_get_ps(rps);
  219. rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
  220. rps->class = le16_to_cpu(non_clock_info->usClassification);
  221. rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
  222. if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
  223. rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
  224. rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
  225. } else {
  226. rps->vclk = 0;
  227. rps->dclk = 0;
  228. }
  229. if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
  230. adev->pm.dpm.boot_ps = rps;
  231. cz_patch_boot_state(adev, ps);
  232. }
  233. if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
  234. adev->pm.dpm.uvd_ps = rps;
  235. }
  236. union power_info {
  237. struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
  238. struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
  239. struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
  240. struct _ATOM_PPLIB_POWERPLAYTABLE4 pplib4;
  241. struct _ATOM_PPLIB_POWERPLAYTABLE5 pplib5;
  242. };
  243. union pplib_power_state {
  244. struct _ATOM_PPLIB_STATE v1;
  245. struct _ATOM_PPLIB_STATE_V2 v2;
  246. };
  247. static int cz_parse_power_table(struct amdgpu_device *adev)
  248. {
  249. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  250. struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
  251. union pplib_power_state *power_state;
  252. int i, j, k, non_clock_array_index, clock_array_index;
  253. union pplib_clock_info *clock_info;
  254. struct _StateArray *state_array;
  255. struct _ClockInfoArray *clock_info_array;
  256. struct _NonClockInfoArray *non_clock_info_array;
  257. union power_info *power_info;
  258. int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
  259. u16 data_offset;
  260. u8 frev, crev;
  261. u8 *power_state_offset;
  262. struct cz_ps *ps;
  263. if (!amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  264. &frev, &crev, &data_offset))
  265. return -EINVAL;
  266. power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
  267. state_array = (struct _StateArray *)
  268. (mode_info->atom_context->bios + data_offset +
  269. le16_to_cpu(power_info->pplib.usStateArrayOffset));
  270. clock_info_array = (struct _ClockInfoArray *)
  271. (mode_info->atom_context->bios + data_offset +
  272. le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
  273. non_clock_info_array = (struct _NonClockInfoArray *)
  274. (mode_info->atom_context->bios + data_offset +
  275. le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
  276. adev->pm.dpm.ps = kzalloc(sizeof(struct amdgpu_ps) *
  277. state_array->ucNumEntries, GFP_KERNEL);
  278. if (!adev->pm.dpm.ps)
  279. return -ENOMEM;
  280. power_state_offset = (u8 *)state_array->states;
  281. adev->pm.dpm.platform_caps =
  282. le32_to_cpu(power_info->pplib.ulPlatformCaps);
  283. adev->pm.dpm.backbias_response_time =
  284. le16_to_cpu(power_info->pplib.usBackbiasTime);
  285. adev->pm.dpm.voltage_response_time =
  286. le16_to_cpu(power_info->pplib.usVoltageTime);
  287. for (i = 0; i < state_array->ucNumEntries; i++) {
  288. power_state = (union pplib_power_state *)power_state_offset;
  289. non_clock_array_index = power_state->v2.nonClockInfoIndex;
  290. non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
  291. &non_clock_info_array->nonClockInfo[non_clock_array_index];
  292. ps = kzalloc(sizeof(struct cz_ps), GFP_KERNEL);
  293. if (ps == NULL) {
  294. kfree(adev->pm.dpm.ps);
  295. return -ENOMEM;
  296. }
  297. adev->pm.dpm.ps[i].ps_priv = ps;
  298. k = 0;
  299. for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
  300. clock_array_index = power_state->v2.clockInfoIndex[j];
  301. if (clock_array_index >= clock_info_array->ucNumEntries)
  302. continue;
  303. if (k >= CZ_MAX_HARDWARE_POWERLEVELS)
  304. break;
  305. clock_info = (union pplib_clock_info *)
  306. &clock_info_array->clockInfo[clock_array_index *
  307. clock_info_array->ucEntrySize];
  308. cz_parse_pplib_clock_info(adev, &adev->pm.dpm.ps[i],
  309. k, clock_info);
  310. k++;
  311. }
  312. cz_parse_pplib_non_clock_info(adev, &adev->pm.dpm.ps[i],
  313. non_clock_info,
  314. non_clock_info_array->ucEntrySize);
  315. power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
  316. }
  317. adev->pm.dpm.num_ps = state_array->ucNumEntries;
  318. return 0;
  319. }
  320. static int cz_process_firmware_header(struct amdgpu_device *adev)
  321. {
  322. struct cz_power_info *pi = cz_get_pi(adev);
  323. u32 tmp;
  324. int ret;
  325. ret = cz_read_smc_sram_dword(adev, SMU8_FIRMWARE_HEADER_LOCATION +
  326. offsetof(struct SMU8_Firmware_Header,
  327. DpmTable),
  328. &tmp, pi->sram_end);
  329. if (ret == 0)
  330. pi->dpm_table_start = tmp;
  331. return ret;
  332. }
  333. static int cz_dpm_init(struct amdgpu_device *adev)
  334. {
  335. struct cz_power_info *pi;
  336. int ret, i;
  337. pi = kzalloc(sizeof(struct cz_power_info), GFP_KERNEL);
  338. if (NULL == pi)
  339. return -ENOMEM;
  340. adev->pm.dpm.priv = pi;
  341. ret = amdgpu_get_platform_caps(adev);
  342. if (ret)
  343. return ret;
  344. ret = amdgpu_parse_extended_power_table(adev);
  345. if (ret)
  346. return ret;
  347. pi->sram_end = SMC_RAM_END;
  348. /* set up DPM defaults */
  349. for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++)
  350. pi->active_target[i] = CZ_AT_DFLT;
  351. pi->mgcg_cgtt_local0 = 0x0;
  352. pi->mgcg_cgtt_local1 = 0x0;
  353. pi->clock_slow_down_step = 25000;
  354. pi->skip_clock_slow_down = 1;
  355. pi->enable_nb_ps_policy = 0;
  356. pi->caps_power_containment = true;
  357. pi->caps_cac = true;
  358. pi->didt_enabled = false;
  359. if (pi->didt_enabled) {
  360. pi->caps_sq_ramping = true;
  361. pi->caps_db_ramping = true;
  362. pi->caps_td_ramping = true;
  363. pi->caps_tcp_ramping = true;
  364. }
  365. pi->caps_sclk_ds = true;
  366. pi->voting_clients = 0x00c00033;
  367. pi->auto_thermal_throttling_enabled = true;
  368. pi->bapm_enabled = false;
  369. pi->disable_nb_ps3_in_battery = false;
  370. pi->voltage_drop_threshold = 0;
  371. pi->caps_sclk_throttle_low_notification = false;
  372. pi->gfx_pg_threshold = 500;
  373. pi->caps_fps = true;
  374. /* uvd */
  375. pi->caps_uvd_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_UVD) ? true : false;
  376. pi->caps_uvd_dpm = true;
  377. /* vce */
  378. pi->caps_vce_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_VCE) ? true : false;
  379. pi->caps_vce_dpm = true;
  380. /* acp */
  381. pi->caps_acp_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_ACP) ? true : false;
  382. pi->caps_acp_dpm = true;
  383. pi->caps_stable_power_state = false;
  384. pi->nb_dpm_enabled_by_driver = true;
  385. pi->nb_dpm_enabled = false;
  386. pi->caps_voltage_island = false;
  387. /* flags which indicate need to upload pptable */
  388. pi->need_pptable_upload = true;
  389. ret = cz_parse_sys_info_table(adev);
  390. if (ret)
  391. return ret;
  392. cz_patch_voltage_values(adev);
  393. cz_construct_boot_state(adev);
  394. ret = cz_parse_power_table(adev);
  395. if (ret)
  396. return ret;
  397. ret = cz_process_firmware_header(adev);
  398. if (ret)
  399. return ret;
  400. pi->dpm_enabled = true;
  401. pi->uvd_dynamic_pg = false;
  402. return 0;
  403. }
  404. static void cz_dpm_fini(struct amdgpu_device *adev)
  405. {
  406. int i;
  407. for (i = 0; i < adev->pm.dpm.num_ps; i++)
  408. kfree(adev->pm.dpm.ps[i].ps_priv);
  409. kfree(adev->pm.dpm.ps);
  410. kfree(adev->pm.dpm.priv);
  411. amdgpu_free_extended_power_table(adev);
  412. }
  413. #define ixSMUSVI_NB_CURRENTVID 0xD8230044
  414. #define CURRENT_NB_VID_MASK 0xff000000
  415. #define CURRENT_NB_VID__SHIFT 24
  416. #define ixSMUSVI_GFX_CURRENTVID 0xD8230048
  417. #define CURRENT_GFX_VID_MASK 0xff000000
  418. #define CURRENT_GFX_VID__SHIFT 24
  419. static void
  420. cz_dpm_debugfs_print_current_performance_level(struct amdgpu_device *adev,
  421. struct seq_file *m)
  422. {
  423. struct cz_power_info *pi = cz_get_pi(adev);
  424. struct amdgpu_clock_voltage_dependency_table *table =
  425. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  426. struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
  427. &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
  428. struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
  429. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  430. u32 sclk_index = REG_GET_FIELD(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX),
  431. TARGET_AND_CURRENT_PROFILE_INDEX, CURR_SCLK_INDEX);
  432. u32 uvd_index = REG_GET_FIELD(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
  433. TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_UVD_INDEX);
  434. u32 vce_index = REG_GET_FIELD(RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX_2),
  435. TARGET_AND_CURRENT_PROFILE_INDEX_2, CURR_VCE_INDEX);
  436. u32 sclk, vclk, dclk, ecclk, tmp;
  437. u16 vddnb, vddgfx;
  438. if (sclk_index >= NUM_SCLK_LEVELS) {
  439. seq_printf(m, "invalid sclk dpm profile %d\n", sclk_index);
  440. } else {
  441. sclk = table->entries[sclk_index].clk;
  442. seq_printf(m, "%u sclk: %u\n", sclk_index, sclk);
  443. }
  444. tmp = (RREG32_SMC(ixSMUSVI_NB_CURRENTVID) &
  445. CURRENT_NB_VID_MASK) >> CURRENT_NB_VID__SHIFT;
  446. vddnb = cz_convert_8bit_index_to_voltage(adev, (u16)tmp);
  447. tmp = (RREG32_SMC(ixSMUSVI_GFX_CURRENTVID) &
  448. CURRENT_GFX_VID_MASK) >> CURRENT_GFX_VID__SHIFT;
  449. vddgfx = cz_convert_8bit_index_to_voltage(adev, (u16)tmp);
  450. seq_printf(m, "vddnb: %u vddgfx: %u\n", vddnb, vddgfx);
  451. seq_printf(m, "uvd %sabled\n", pi->uvd_power_gated ? "dis" : "en");
  452. if (!pi->uvd_power_gated) {
  453. if (uvd_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
  454. seq_printf(m, "invalid uvd dpm level %d\n", uvd_index);
  455. } else {
  456. vclk = uvd_table->entries[uvd_index].vclk;
  457. dclk = uvd_table->entries[uvd_index].dclk;
  458. seq_printf(m, "%u uvd vclk: %u dclk: %u\n", uvd_index, vclk, dclk);
  459. }
  460. }
  461. seq_printf(m, "vce %sabled\n", pi->vce_power_gated ? "dis" : "en");
  462. if (!pi->vce_power_gated) {
  463. if (vce_index >= CZ_MAX_HARDWARE_POWERLEVELS) {
  464. seq_printf(m, "invalid vce dpm level %d\n", vce_index);
  465. } else {
  466. ecclk = vce_table->entries[vce_index].ecclk;
  467. seq_printf(m, "%u vce ecclk: %u\n", vce_index, ecclk);
  468. }
  469. }
  470. }
  471. static void cz_dpm_print_power_state(struct amdgpu_device *adev,
  472. struct amdgpu_ps *rps)
  473. {
  474. int i;
  475. struct cz_ps *ps = cz_get_ps(rps);
  476. amdgpu_dpm_print_class_info(rps->class, rps->class2);
  477. amdgpu_dpm_print_cap_info(rps->caps);
  478. DRM_INFO("\tuvd vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
  479. for (i = 0; i < ps->num_levels; i++) {
  480. struct cz_pl *pl = &ps->levels[i];
  481. DRM_INFO("\t\tpower level %d sclk: %u vddc: %u\n",
  482. i, pl->sclk,
  483. cz_convert_8bit_index_to_voltage(adev, pl->vddc_index));
  484. }
  485. amdgpu_dpm_print_ps_status(adev, rps);
  486. }
  487. static void cz_dpm_set_funcs(struct amdgpu_device *adev);
  488. static int cz_dpm_early_init(void *handle)
  489. {
  490. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  491. cz_dpm_set_funcs(adev);
  492. return 0;
  493. }
  494. static int cz_dpm_late_init(void *handle)
  495. {
  496. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  497. if (amdgpu_dpm) {
  498. int ret;
  499. /* init the sysfs and debugfs files late */
  500. ret = amdgpu_pm_sysfs_init(adev);
  501. if (ret)
  502. return ret;
  503. /* powerdown unused blocks for now */
  504. cz_dpm_powergate_uvd(adev, true);
  505. cz_dpm_powergate_vce(adev, true);
  506. }
  507. return 0;
  508. }
  509. static int cz_dpm_sw_init(void *handle)
  510. {
  511. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  512. int ret = 0;
  513. /* fix me to add thermal support TODO */
  514. /* default to balanced state */
  515. adev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
  516. adev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
  517. adev->pm.dpm.forced_level = AMDGPU_DPM_FORCED_LEVEL_AUTO;
  518. adev->pm.default_sclk = adev->clock.default_sclk;
  519. adev->pm.default_mclk = adev->clock.default_mclk;
  520. adev->pm.current_sclk = adev->clock.default_sclk;
  521. adev->pm.current_mclk = adev->clock.default_mclk;
  522. adev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  523. if (amdgpu_dpm == 0)
  524. return 0;
  525. mutex_lock(&adev->pm.mutex);
  526. ret = cz_dpm_init(adev);
  527. if (ret)
  528. goto dpm_init_failed;
  529. adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps = adev->pm.dpm.boot_ps;
  530. if (amdgpu_dpm == 1)
  531. amdgpu_pm_print_power_states(adev);
  532. mutex_unlock(&adev->pm.mutex);
  533. DRM_INFO("amdgpu: dpm initialized\n");
  534. return 0;
  535. dpm_init_failed:
  536. cz_dpm_fini(adev);
  537. mutex_unlock(&adev->pm.mutex);
  538. DRM_ERROR("amdgpu: dpm initialization failed\n");
  539. return ret;
  540. }
  541. static int cz_dpm_sw_fini(void *handle)
  542. {
  543. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  544. mutex_lock(&adev->pm.mutex);
  545. amdgpu_pm_sysfs_fini(adev);
  546. cz_dpm_fini(adev);
  547. mutex_unlock(&adev->pm.mutex);
  548. return 0;
  549. }
  550. static void cz_reset_ap_mask(struct amdgpu_device *adev)
  551. {
  552. struct cz_power_info *pi = cz_get_pi(adev);
  553. pi->active_process_mask = 0;
  554. }
  555. static int cz_dpm_download_pptable_from_smu(struct amdgpu_device *adev,
  556. void **table)
  557. {
  558. int ret = 0;
  559. ret = cz_smu_download_pptable(adev, table);
  560. return ret;
  561. }
  562. static int cz_dpm_upload_pptable_to_smu(struct amdgpu_device *adev)
  563. {
  564. struct cz_power_info *pi = cz_get_pi(adev);
  565. struct SMU8_Fusion_ClkTable *clock_table;
  566. struct atom_clock_dividers dividers;
  567. void *table = NULL;
  568. uint8_t i = 0;
  569. int ret = 0;
  570. struct amdgpu_clock_voltage_dependency_table *vddc_table =
  571. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  572. struct amdgpu_clock_voltage_dependency_table *vddgfx_table =
  573. &adev->pm.dpm.dyn_state.vddgfx_dependency_on_sclk;
  574. struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
  575. &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
  576. struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
  577. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  578. struct amdgpu_clock_voltage_dependency_table *acp_table =
  579. &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
  580. if (!pi->need_pptable_upload)
  581. return 0;
  582. ret = cz_dpm_download_pptable_from_smu(adev, &table);
  583. if (ret) {
  584. DRM_ERROR("amdgpu: Failed to get power play table from SMU!\n");
  585. return -EINVAL;
  586. }
  587. clock_table = (struct SMU8_Fusion_ClkTable *)table;
  588. /* patch clock table */
  589. if (vddc_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
  590. vddgfx_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
  591. uvd_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
  592. vce_table->count > CZ_MAX_HARDWARE_POWERLEVELS ||
  593. acp_table->count > CZ_MAX_HARDWARE_POWERLEVELS) {
  594. DRM_ERROR("amdgpu: Invalid Clock Voltage Dependency Table!\n");
  595. return -EINVAL;
  596. }
  597. for (i = 0; i < CZ_MAX_HARDWARE_POWERLEVELS; i++) {
  598. /* vddc sclk */
  599. clock_table->SclkBreakdownTable.ClkLevel[i].GnbVid =
  600. (i < vddc_table->count) ? (uint8_t)vddc_table->entries[i].v : 0;
  601. clock_table->SclkBreakdownTable.ClkLevel[i].Frequency =
  602. (i < vddc_table->count) ? vddc_table->entries[i].clk : 0;
  603. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  604. clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
  605. false, &dividers);
  606. if (ret)
  607. return ret;
  608. clock_table->SclkBreakdownTable.ClkLevel[i].DfsDid =
  609. (uint8_t)dividers.post_divider;
  610. /* vddgfx sclk */
  611. clock_table->SclkBreakdownTable.ClkLevel[i].GfxVid =
  612. (i < vddgfx_table->count) ? (uint8_t)vddgfx_table->entries[i].v : 0;
  613. /* acp breakdown */
  614. clock_table->AclkBreakdownTable.ClkLevel[i].GfxVid =
  615. (i < acp_table->count) ? (uint8_t)acp_table->entries[i].v : 0;
  616. clock_table->AclkBreakdownTable.ClkLevel[i].Frequency =
  617. (i < acp_table->count) ? acp_table->entries[i].clk : 0;
  618. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  619. clock_table->SclkBreakdownTable.ClkLevel[i].Frequency,
  620. false, &dividers);
  621. if (ret)
  622. return ret;
  623. clock_table->AclkBreakdownTable.ClkLevel[i].DfsDid =
  624. (uint8_t)dividers.post_divider;
  625. /* uvd breakdown */
  626. clock_table->VclkBreakdownTable.ClkLevel[i].GfxVid =
  627. (i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
  628. clock_table->VclkBreakdownTable.ClkLevel[i].Frequency =
  629. (i < uvd_table->count) ? uvd_table->entries[i].vclk : 0;
  630. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  631. clock_table->VclkBreakdownTable.ClkLevel[i].Frequency,
  632. false, &dividers);
  633. if (ret)
  634. return ret;
  635. clock_table->VclkBreakdownTable.ClkLevel[i].DfsDid =
  636. (uint8_t)dividers.post_divider;
  637. clock_table->DclkBreakdownTable.ClkLevel[i].GfxVid =
  638. (i < uvd_table->count) ? (uint8_t)uvd_table->entries[i].v : 0;
  639. clock_table->DclkBreakdownTable.ClkLevel[i].Frequency =
  640. (i < uvd_table->count) ? uvd_table->entries[i].dclk : 0;
  641. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  642. clock_table->DclkBreakdownTable.ClkLevel[i].Frequency,
  643. false, &dividers);
  644. if (ret)
  645. return ret;
  646. clock_table->DclkBreakdownTable.ClkLevel[i].DfsDid =
  647. (uint8_t)dividers.post_divider;
  648. /* vce breakdown */
  649. clock_table->EclkBreakdownTable.ClkLevel[i].GfxVid =
  650. (i < vce_table->count) ? (uint8_t)vce_table->entries[i].v : 0;
  651. clock_table->EclkBreakdownTable.ClkLevel[i].Frequency =
  652. (i < vce_table->count) ? vce_table->entries[i].ecclk : 0;
  653. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  654. clock_table->EclkBreakdownTable.ClkLevel[i].Frequency,
  655. false, &dividers);
  656. if (ret)
  657. return ret;
  658. clock_table->EclkBreakdownTable.ClkLevel[i].DfsDid =
  659. (uint8_t)dividers.post_divider;
  660. }
  661. /* its time to upload to SMU */
  662. ret = cz_smu_upload_pptable(adev);
  663. if (ret) {
  664. DRM_ERROR("amdgpu: Failed to put power play table to SMU!\n");
  665. return ret;
  666. }
  667. return 0;
  668. }
  669. static void cz_init_sclk_limit(struct amdgpu_device *adev)
  670. {
  671. struct cz_power_info *pi = cz_get_pi(adev);
  672. struct amdgpu_clock_voltage_dependency_table *table =
  673. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  674. uint32_t clock = 0, level;
  675. if (!table || !table->count) {
  676. DRM_ERROR("Invalid Voltage Dependency table.\n");
  677. return;
  678. }
  679. pi->sclk_dpm.soft_min_clk = 0;
  680. pi->sclk_dpm.hard_min_clk = 0;
  681. cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxSclkLevel);
  682. level = cz_get_argument(adev);
  683. if (level < table->count)
  684. clock = table->entries[level].clk;
  685. else {
  686. DRM_ERROR("Invalid SLCK Voltage Dependency table entry.\n");
  687. clock = table->entries[table->count - 1].clk;
  688. }
  689. pi->sclk_dpm.soft_max_clk = clock;
  690. pi->sclk_dpm.hard_max_clk = clock;
  691. }
  692. static void cz_init_uvd_limit(struct amdgpu_device *adev)
  693. {
  694. struct cz_power_info *pi = cz_get_pi(adev);
  695. struct amdgpu_uvd_clock_voltage_dependency_table *table =
  696. &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
  697. uint32_t clock = 0, level;
  698. if (!table || !table->count) {
  699. DRM_ERROR("Invalid Voltage Dependency table.\n");
  700. return;
  701. }
  702. pi->uvd_dpm.soft_min_clk = 0;
  703. pi->uvd_dpm.hard_min_clk = 0;
  704. cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxUvdLevel);
  705. level = cz_get_argument(adev);
  706. if (level < table->count)
  707. clock = table->entries[level].vclk;
  708. else {
  709. DRM_ERROR("Invalid UVD Voltage Dependency table entry.\n");
  710. clock = table->entries[table->count - 1].vclk;
  711. }
  712. pi->uvd_dpm.soft_max_clk = clock;
  713. pi->uvd_dpm.hard_max_clk = clock;
  714. }
  715. static void cz_init_vce_limit(struct amdgpu_device *adev)
  716. {
  717. struct cz_power_info *pi = cz_get_pi(adev);
  718. struct amdgpu_vce_clock_voltage_dependency_table *table =
  719. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  720. uint32_t clock = 0, level;
  721. if (!table || !table->count) {
  722. DRM_ERROR("Invalid Voltage Dependency table.\n");
  723. return;
  724. }
  725. pi->vce_dpm.soft_min_clk = table->entries[0].ecclk;
  726. pi->vce_dpm.hard_min_clk = table->entries[0].ecclk;
  727. cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxEclkLevel);
  728. level = cz_get_argument(adev);
  729. if (level < table->count)
  730. clock = table->entries[level].ecclk;
  731. else {
  732. /* future BIOS would fix this error */
  733. DRM_ERROR("Invalid VCE Voltage Dependency table entry.\n");
  734. clock = table->entries[table->count - 1].ecclk;
  735. }
  736. pi->vce_dpm.soft_max_clk = clock;
  737. pi->vce_dpm.hard_max_clk = clock;
  738. }
  739. static void cz_init_acp_limit(struct amdgpu_device *adev)
  740. {
  741. struct cz_power_info *pi = cz_get_pi(adev);
  742. struct amdgpu_clock_voltage_dependency_table *table =
  743. &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
  744. uint32_t clock = 0, level;
  745. if (!table || !table->count) {
  746. DRM_ERROR("Invalid Voltage Dependency table.\n");
  747. return;
  748. }
  749. pi->acp_dpm.soft_min_clk = 0;
  750. pi->acp_dpm.hard_min_clk = 0;
  751. cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxAclkLevel);
  752. level = cz_get_argument(adev);
  753. if (level < table->count)
  754. clock = table->entries[level].clk;
  755. else {
  756. DRM_ERROR("Invalid ACP Voltage Dependency table entry.\n");
  757. clock = table->entries[table->count - 1].clk;
  758. }
  759. pi->acp_dpm.soft_max_clk = clock;
  760. pi->acp_dpm.hard_max_clk = clock;
  761. }
  762. static void cz_init_pg_state(struct amdgpu_device *adev)
  763. {
  764. struct cz_power_info *pi = cz_get_pi(adev);
  765. pi->uvd_power_gated = false;
  766. pi->vce_power_gated = false;
  767. pi->acp_power_gated = false;
  768. }
  769. static void cz_init_sclk_threshold(struct amdgpu_device *adev)
  770. {
  771. struct cz_power_info *pi = cz_get_pi(adev);
  772. pi->low_sclk_interrupt_threshold = 0;
  773. }
  774. static void cz_dpm_setup_asic(struct amdgpu_device *adev)
  775. {
  776. cz_reset_ap_mask(adev);
  777. cz_dpm_upload_pptable_to_smu(adev);
  778. cz_init_sclk_limit(adev);
  779. cz_init_uvd_limit(adev);
  780. cz_init_vce_limit(adev);
  781. cz_init_acp_limit(adev);
  782. cz_init_pg_state(adev);
  783. cz_init_sclk_threshold(adev);
  784. }
  785. static bool cz_check_smu_feature(struct amdgpu_device *adev,
  786. uint32_t feature)
  787. {
  788. uint32_t smu_feature = 0;
  789. int ret;
  790. ret = cz_send_msg_to_smc_with_parameter(adev,
  791. PPSMC_MSG_GetFeatureStatus, 0);
  792. if (ret) {
  793. DRM_ERROR("Failed to get SMU features from SMC.\n");
  794. return false;
  795. } else {
  796. smu_feature = cz_get_argument(adev);
  797. if (feature & smu_feature)
  798. return true;
  799. }
  800. return false;
  801. }
  802. static bool cz_check_for_dpm_enabled(struct amdgpu_device *adev)
  803. {
  804. if (cz_check_smu_feature(adev,
  805. SMU_EnabledFeatureScoreboard_SclkDpmOn))
  806. return true;
  807. return false;
  808. }
  809. static void cz_program_voting_clients(struct amdgpu_device *adev)
  810. {
  811. WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, PPCZ_VOTINGRIGHTSCLIENTS_DFLT0);
  812. }
  813. static void cz_clear_voting_clients(struct amdgpu_device *adev)
  814. {
  815. WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, 0);
  816. }
  817. static int cz_start_dpm(struct amdgpu_device *adev)
  818. {
  819. int ret = 0;
  820. if (amdgpu_dpm) {
  821. ret = cz_send_msg_to_smc_with_parameter(adev,
  822. PPSMC_MSG_EnableAllSmuFeatures, SCLK_DPM_MASK);
  823. if (ret) {
  824. DRM_ERROR("SMU feature: SCLK_DPM enable failed\n");
  825. return -EINVAL;
  826. }
  827. }
  828. return 0;
  829. }
  830. static int cz_stop_dpm(struct amdgpu_device *adev)
  831. {
  832. int ret = 0;
  833. if (amdgpu_dpm && adev->pm.dpm_enabled) {
  834. ret = cz_send_msg_to_smc_with_parameter(adev,
  835. PPSMC_MSG_DisableAllSmuFeatures, SCLK_DPM_MASK);
  836. if (ret) {
  837. DRM_ERROR("SMU feature: SCLK_DPM disable failed\n");
  838. return -EINVAL;
  839. }
  840. }
  841. return 0;
  842. }
  843. static uint32_t cz_get_sclk_level(struct amdgpu_device *adev,
  844. uint32_t clock, uint16_t msg)
  845. {
  846. int i = 0;
  847. struct amdgpu_clock_voltage_dependency_table *table =
  848. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  849. switch (msg) {
  850. case PPSMC_MSG_SetSclkSoftMin:
  851. case PPSMC_MSG_SetSclkHardMin:
  852. for (i = 0; i < table->count; i++)
  853. if (clock <= table->entries[i].clk)
  854. break;
  855. if (i == table->count)
  856. i = table->count - 1;
  857. break;
  858. case PPSMC_MSG_SetSclkSoftMax:
  859. case PPSMC_MSG_SetSclkHardMax:
  860. for (i = table->count - 1; i >= 0; i--)
  861. if (clock >= table->entries[i].clk)
  862. break;
  863. if (i < 0)
  864. i = 0;
  865. break;
  866. default:
  867. break;
  868. }
  869. return i;
  870. }
  871. static uint32_t cz_get_eclk_level(struct amdgpu_device *adev,
  872. uint32_t clock, uint16_t msg)
  873. {
  874. int i = 0;
  875. struct amdgpu_vce_clock_voltage_dependency_table *table =
  876. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  877. if (table->count == 0)
  878. return 0;
  879. switch (msg) {
  880. case PPSMC_MSG_SetEclkSoftMin:
  881. case PPSMC_MSG_SetEclkHardMin:
  882. for (i = 0; i < table->count-1; i++)
  883. if (clock <= table->entries[i].ecclk)
  884. break;
  885. break;
  886. case PPSMC_MSG_SetEclkSoftMax:
  887. case PPSMC_MSG_SetEclkHardMax:
  888. for (i = table->count - 1; i > 0; i--)
  889. if (clock >= table->entries[i].ecclk)
  890. break;
  891. break;
  892. default:
  893. break;
  894. }
  895. return i;
  896. }
  897. static int cz_program_bootup_state(struct amdgpu_device *adev)
  898. {
  899. struct cz_power_info *pi = cz_get_pi(adev);
  900. uint32_t soft_min_clk = 0;
  901. uint32_t soft_max_clk = 0;
  902. int ret = 0;
  903. pi->sclk_dpm.soft_min_clk = pi->sys_info.bootup_sclk;
  904. pi->sclk_dpm.soft_max_clk = pi->sys_info.bootup_sclk;
  905. soft_min_clk = cz_get_sclk_level(adev,
  906. pi->sclk_dpm.soft_min_clk,
  907. PPSMC_MSG_SetSclkSoftMin);
  908. soft_max_clk = cz_get_sclk_level(adev,
  909. pi->sclk_dpm.soft_max_clk,
  910. PPSMC_MSG_SetSclkSoftMax);
  911. ret = cz_send_msg_to_smc_with_parameter(adev,
  912. PPSMC_MSG_SetSclkSoftMin, soft_min_clk);
  913. if (ret)
  914. return -EINVAL;
  915. ret = cz_send_msg_to_smc_with_parameter(adev,
  916. PPSMC_MSG_SetSclkSoftMax, soft_max_clk);
  917. if (ret)
  918. return -EINVAL;
  919. return 0;
  920. }
  921. /* TODO */
  922. static int cz_disable_cgpg(struct amdgpu_device *adev)
  923. {
  924. return 0;
  925. }
  926. /* TODO */
  927. static int cz_enable_cgpg(struct amdgpu_device *adev)
  928. {
  929. return 0;
  930. }
  931. /* TODO */
  932. static int cz_program_pt_config_registers(struct amdgpu_device *adev)
  933. {
  934. return 0;
  935. }
  936. static void cz_do_enable_didt(struct amdgpu_device *adev, bool enable)
  937. {
  938. struct cz_power_info *pi = cz_get_pi(adev);
  939. uint32_t reg = 0;
  940. if (pi->caps_sq_ramping) {
  941. reg = RREG32_DIDT(ixDIDT_SQ_CTRL0);
  942. if (enable)
  943. reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 1);
  944. else
  945. reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 0);
  946. WREG32_DIDT(ixDIDT_SQ_CTRL0, reg);
  947. }
  948. if (pi->caps_db_ramping) {
  949. reg = RREG32_DIDT(ixDIDT_DB_CTRL0);
  950. if (enable)
  951. reg = REG_SET_FIELD(reg, DIDT_DB_CTRL0, DIDT_CTRL_EN, 1);
  952. else
  953. reg = REG_SET_FIELD(reg, DIDT_DB_CTRL0, DIDT_CTRL_EN, 0);
  954. WREG32_DIDT(ixDIDT_DB_CTRL0, reg);
  955. }
  956. if (pi->caps_td_ramping) {
  957. reg = RREG32_DIDT(ixDIDT_TD_CTRL0);
  958. if (enable)
  959. reg = REG_SET_FIELD(reg, DIDT_TD_CTRL0, DIDT_CTRL_EN, 1);
  960. else
  961. reg = REG_SET_FIELD(reg, DIDT_TD_CTRL0, DIDT_CTRL_EN, 0);
  962. WREG32_DIDT(ixDIDT_TD_CTRL0, reg);
  963. }
  964. if (pi->caps_tcp_ramping) {
  965. reg = RREG32_DIDT(ixDIDT_TCP_CTRL0);
  966. if (enable)
  967. reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 1);
  968. else
  969. reg = REG_SET_FIELD(reg, DIDT_SQ_CTRL0, DIDT_CTRL_EN, 0);
  970. WREG32_DIDT(ixDIDT_TCP_CTRL0, reg);
  971. }
  972. }
  973. static int cz_enable_didt(struct amdgpu_device *adev, bool enable)
  974. {
  975. struct cz_power_info *pi = cz_get_pi(adev);
  976. int ret;
  977. if (pi->caps_sq_ramping || pi->caps_db_ramping ||
  978. pi->caps_td_ramping || pi->caps_tcp_ramping) {
  979. if (adev->gfx.gfx_current_status != AMDGPU_GFX_SAFE_MODE) {
  980. ret = cz_disable_cgpg(adev);
  981. if (ret) {
  982. DRM_ERROR("Pre Di/Dt disable cg/pg failed\n");
  983. return -EINVAL;
  984. }
  985. adev->gfx.gfx_current_status = AMDGPU_GFX_SAFE_MODE;
  986. }
  987. ret = cz_program_pt_config_registers(adev);
  988. if (ret) {
  989. DRM_ERROR("Di/Dt config failed\n");
  990. return -EINVAL;
  991. }
  992. cz_do_enable_didt(adev, enable);
  993. if (adev->gfx.gfx_current_status == AMDGPU_GFX_SAFE_MODE) {
  994. ret = cz_enable_cgpg(adev);
  995. if (ret) {
  996. DRM_ERROR("Post Di/Dt enable cg/pg failed\n");
  997. return -EINVAL;
  998. }
  999. adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
  1000. }
  1001. }
  1002. return 0;
  1003. }
  1004. /* TODO */
  1005. static void cz_reset_acp_boot_level(struct amdgpu_device *adev)
  1006. {
  1007. }
  1008. static void cz_update_current_ps(struct amdgpu_device *adev,
  1009. struct amdgpu_ps *rps)
  1010. {
  1011. struct cz_power_info *pi = cz_get_pi(adev);
  1012. struct cz_ps *ps = cz_get_ps(rps);
  1013. pi->current_ps = *ps;
  1014. pi->current_rps = *rps;
  1015. pi->current_rps.ps_priv = ps;
  1016. }
  1017. static void cz_update_requested_ps(struct amdgpu_device *adev,
  1018. struct amdgpu_ps *rps)
  1019. {
  1020. struct cz_power_info *pi = cz_get_pi(adev);
  1021. struct cz_ps *ps = cz_get_ps(rps);
  1022. pi->requested_ps = *ps;
  1023. pi->requested_rps = *rps;
  1024. pi->requested_rps.ps_priv = ps;
  1025. }
  1026. /* PP arbiter support needed TODO */
  1027. static void cz_apply_state_adjust_rules(struct amdgpu_device *adev,
  1028. struct amdgpu_ps *new_rps,
  1029. struct amdgpu_ps *old_rps)
  1030. {
  1031. struct cz_ps *ps = cz_get_ps(new_rps);
  1032. struct cz_power_info *pi = cz_get_pi(adev);
  1033. struct amdgpu_clock_and_voltage_limits *limits =
  1034. &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  1035. /* 10kHz memory clock */
  1036. uint32_t mclk = 0;
  1037. ps->force_high = false;
  1038. ps->need_dfs_bypass = true;
  1039. pi->video_start = new_rps->dclk || new_rps->vclk ||
  1040. new_rps->evclk || new_rps->ecclk;
  1041. if ((new_rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
  1042. ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
  1043. pi->battery_state = true;
  1044. else
  1045. pi->battery_state = false;
  1046. if (pi->caps_stable_power_state)
  1047. mclk = limits->mclk;
  1048. if (mclk > pi->sys_info.nbp_memory_clock[CZ_NUM_NBPMEMORY_CLOCK - 1])
  1049. ps->force_high = true;
  1050. }
  1051. static int cz_dpm_enable(struct amdgpu_device *adev)
  1052. {
  1053. const char *chip_name;
  1054. int ret = 0;
  1055. /* renable will hang up SMU, so check first */
  1056. if (cz_check_for_dpm_enabled(adev))
  1057. return -EINVAL;
  1058. cz_program_voting_clients(adev);
  1059. switch (adev->asic_type) {
  1060. case CHIP_CARRIZO:
  1061. chip_name = "carrizo";
  1062. break;
  1063. case CHIP_STONEY:
  1064. chip_name = "stoney";
  1065. break;
  1066. default:
  1067. BUG();
  1068. }
  1069. ret = cz_start_dpm(adev);
  1070. if (ret) {
  1071. DRM_ERROR("%s DPM enable failed\n", chip_name);
  1072. return -EINVAL;
  1073. }
  1074. ret = cz_program_bootup_state(adev);
  1075. if (ret) {
  1076. DRM_ERROR("%s bootup state program failed\n", chip_name);
  1077. return -EINVAL;
  1078. }
  1079. ret = cz_enable_didt(adev, true);
  1080. if (ret) {
  1081. DRM_ERROR("%s enable di/dt failed\n", chip_name);
  1082. return -EINVAL;
  1083. }
  1084. cz_reset_acp_boot_level(adev);
  1085. cz_update_current_ps(adev, adev->pm.dpm.boot_ps);
  1086. return 0;
  1087. }
  1088. static int cz_dpm_hw_init(void *handle)
  1089. {
  1090. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1091. int ret = 0;
  1092. mutex_lock(&adev->pm.mutex);
  1093. /* smu init only needs to be called at startup, not resume.
  1094. * It should be in sw_init, but requires the fw info gathered
  1095. * in sw_init from other IP modules.
  1096. */
  1097. ret = cz_smu_init(adev);
  1098. if (ret) {
  1099. DRM_ERROR("amdgpu: smc initialization failed\n");
  1100. mutex_unlock(&adev->pm.mutex);
  1101. return ret;
  1102. }
  1103. /* do the actual fw loading */
  1104. ret = cz_smu_start(adev);
  1105. if (ret) {
  1106. DRM_ERROR("amdgpu: smc start failed\n");
  1107. mutex_unlock(&adev->pm.mutex);
  1108. return ret;
  1109. }
  1110. if (!amdgpu_dpm) {
  1111. adev->pm.dpm_enabled = false;
  1112. mutex_unlock(&adev->pm.mutex);
  1113. return ret;
  1114. }
  1115. /* cz dpm setup asic */
  1116. cz_dpm_setup_asic(adev);
  1117. /* cz dpm enable */
  1118. ret = cz_dpm_enable(adev);
  1119. if (ret)
  1120. adev->pm.dpm_enabled = false;
  1121. else
  1122. adev->pm.dpm_enabled = true;
  1123. mutex_unlock(&adev->pm.mutex);
  1124. return 0;
  1125. }
  1126. static int cz_dpm_disable(struct amdgpu_device *adev)
  1127. {
  1128. int ret = 0;
  1129. if (!cz_check_for_dpm_enabled(adev))
  1130. return -EINVAL;
  1131. ret = cz_enable_didt(adev, false);
  1132. if (ret) {
  1133. DRM_ERROR("disable di/dt failed\n");
  1134. return -EINVAL;
  1135. }
  1136. /* powerup blocks */
  1137. cz_dpm_powergate_uvd(adev, false);
  1138. cz_dpm_powergate_vce(adev, false);
  1139. cz_clear_voting_clients(adev);
  1140. cz_stop_dpm(adev);
  1141. cz_update_current_ps(adev, adev->pm.dpm.boot_ps);
  1142. return 0;
  1143. }
  1144. static int cz_dpm_hw_fini(void *handle)
  1145. {
  1146. int ret = 0;
  1147. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1148. mutex_lock(&adev->pm.mutex);
  1149. /* smu fini only needs to be called at teardown, not suspend.
  1150. * It should be in sw_fini, but we put it here for symmetry
  1151. * with smu init.
  1152. */
  1153. cz_smu_fini(adev);
  1154. if (adev->pm.dpm_enabled) {
  1155. ret = cz_dpm_disable(adev);
  1156. adev->pm.dpm.current_ps =
  1157. adev->pm.dpm.requested_ps =
  1158. adev->pm.dpm.boot_ps;
  1159. }
  1160. adev->pm.dpm_enabled = false;
  1161. mutex_unlock(&adev->pm.mutex);
  1162. return ret;
  1163. }
  1164. static int cz_dpm_suspend(void *handle)
  1165. {
  1166. int ret = 0;
  1167. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1168. if (adev->pm.dpm_enabled) {
  1169. mutex_lock(&adev->pm.mutex);
  1170. ret = cz_dpm_disable(adev);
  1171. adev->pm.dpm.current_ps =
  1172. adev->pm.dpm.requested_ps =
  1173. adev->pm.dpm.boot_ps;
  1174. mutex_unlock(&adev->pm.mutex);
  1175. }
  1176. return ret;
  1177. }
  1178. static int cz_dpm_resume(void *handle)
  1179. {
  1180. int ret = 0;
  1181. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1182. mutex_lock(&adev->pm.mutex);
  1183. /* do the actual fw loading */
  1184. ret = cz_smu_start(adev);
  1185. if (ret) {
  1186. DRM_ERROR("amdgpu: smc start failed\n");
  1187. mutex_unlock(&adev->pm.mutex);
  1188. return ret;
  1189. }
  1190. if (!amdgpu_dpm) {
  1191. adev->pm.dpm_enabled = false;
  1192. mutex_unlock(&adev->pm.mutex);
  1193. return ret;
  1194. }
  1195. /* cz dpm setup asic */
  1196. cz_dpm_setup_asic(adev);
  1197. /* cz dpm enable */
  1198. ret = cz_dpm_enable(adev);
  1199. if (ret)
  1200. adev->pm.dpm_enabled = false;
  1201. else
  1202. adev->pm.dpm_enabled = true;
  1203. mutex_unlock(&adev->pm.mutex);
  1204. /* upon resume, re-compute the clocks */
  1205. if (adev->pm.dpm_enabled)
  1206. amdgpu_pm_compute_clocks(adev);
  1207. return 0;
  1208. }
  1209. static int cz_dpm_set_clockgating_state(void *handle,
  1210. enum amd_clockgating_state state)
  1211. {
  1212. return 0;
  1213. }
  1214. static int cz_dpm_set_powergating_state(void *handle,
  1215. enum amd_powergating_state state)
  1216. {
  1217. return 0;
  1218. }
  1219. /* borrowed from KV, need future unify */
  1220. static int cz_dpm_get_temperature(struct amdgpu_device *adev)
  1221. {
  1222. int actual_temp = 0;
  1223. uint32_t temp = RREG32_SMC(0xC0300E0C);
  1224. if (temp)
  1225. actual_temp = 1000 * ((temp / 8) - 49);
  1226. return actual_temp;
  1227. }
  1228. static int cz_dpm_pre_set_power_state(struct amdgpu_device *adev)
  1229. {
  1230. struct cz_power_info *pi = cz_get_pi(adev);
  1231. struct amdgpu_ps requested_ps = *adev->pm.dpm.requested_ps;
  1232. struct amdgpu_ps *new_ps = &requested_ps;
  1233. cz_update_requested_ps(adev, new_ps);
  1234. cz_apply_state_adjust_rules(adev, &pi->requested_rps,
  1235. &pi->current_rps);
  1236. return 0;
  1237. }
  1238. static int cz_dpm_update_sclk_limit(struct amdgpu_device *adev)
  1239. {
  1240. struct cz_power_info *pi = cz_get_pi(adev);
  1241. struct amdgpu_clock_and_voltage_limits *limits =
  1242. &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  1243. uint32_t clock, stable_ps_clock = 0;
  1244. clock = pi->sclk_dpm.soft_min_clk;
  1245. if (pi->caps_stable_power_state) {
  1246. stable_ps_clock = limits->sclk * 75 / 100;
  1247. if (clock < stable_ps_clock)
  1248. clock = stable_ps_clock;
  1249. }
  1250. if (clock != pi->sclk_dpm.soft_min_clk) {
  1251. pi->sclk_dpm.soft_min_clk = clock;
  1252. cz_send_msg_to_smc_with_parameter(adev,
  1253. PPSMC_MSG_SetSclkSoftMin,
  1254. cz_get_sclk_level(adev, clock,
  1255. PPSMC_MSG_SetSclkSoftMin));
  1256. }
  1257. if (pi->caps_stable_power_state &&
  1258. pi->sclk_dpm.soft_max_clk != clock) {
  1259. pi->sclk_dpm.soft_max_clk = clock;
  1260. cz_send_msg_to_smc_with_parameter(adev,
  1261. PPSMC_MSG_SetSclkSoftMax,
  1262. cz_get_sclk_level(adev, clock,
  1263. PPSMC_MSG_SetSclkSoftMax));
  1264. } else {
  1265. cz_send_msg_to_smc_with_parameter(adev,
  1266. PPSMC_MSG_SetSclkSoftMax,
  1267. cz_get_sclk_level(adev,
  1268. pi->sclk_dpm.soft_max_clk,
  1269. PPSMC_MSG_SetSclkSoftMax));
  1270. }
  1271. return 0;
  1272. }
  1273. static int cz_dpm_set_deep_sleep_sclk_threshold(struct amdgpu_device *adev)
  1274. {
  1275. int ret = 0;
  1276. struct cz_power_info *pi = cz_get_pi(adev);
  1277. if (pi->caps_sclk_ds) {
  1278. cz_send_msg_to_smc_with_parameter(adev,
  1279. PPSMC_MSG_SetMinDeepSleepSclk,
  1280. CZ_MIN_DEEP_SLEEP_SCLK);
  1281. }
  1282. return ret;
  1283. }
  1284. /* ?? without dal support, is this still needed in setpowerstate list*/
  1285. static int cz_dpm_set_watermark_threshold(struct amdgpu_device *adev)
  1286. {
  1287. int ret = 0;
  1288. struct cz_power_info *pi = cz_get_pi(adev);
  1289. cz_send_msg_to_smc_with_parameter(adev,
  1290. PPSMC_MSG_SetWatermarkFrequency,
  1291. pi->sclk_dpm.soft_max_clk);
  1292. return ret;
  1293. }
  1294. static int cz_dpm_enable_nbdpm(struct amdgpu_device *adev)
  1295. {
  1296. int ret = 0;
  1297. struct cz_power_info *pi = cz_get_pi(adev);
  1298. /* also depend on dal NBPStateDisableRequired */
  1299. if (pi->nb_dpm_enabled_by_driver && !pi->nb_dpm_enabled) {
  1300. ret = cz_send_msg_to_smc_with_parameter(adev,
  1301. PPSMC_MSG_EnableAllSmuFeatures,
  1302. NB_DPM_MASK);
  1303. if (ret) {
  1304. DRM_ERROR("amdgpu: nb dpm enable failed\n");
  1305. return ret;
  1306. }
  1307. pi->nb_dpm_enabled = true;
  1308. }
  1309. return ret;
  1310. }
  1311. static void cz_dpm_nbdpm_lm_pstate_enable(struct amdgpu_device *adev,
  1312. bool enable)
  1313. {
  1314. if (enable)
  1315. cz_send_msg_to_smc(adev, PPSMC_MSG_EnableLowMemoryPstate);
  1316. else
  1317. cz_send_msg_to_smc(adev, PPSMC_MSG_DisableLowMemoryPstate);
  1318. }
  1319. static int cz_dpm_update_low_memory_pstate(struct amdgpu_device *adev)
  1320. {
  1321. int ret = 0;
  1322. struct cz_power_info *pi = cz_get_pi(adev);
  1323. struct cz_ps *ps = &pi->requested_ps;
  1324. if (pi->sys_info.nb_dpm_enable) {
  1325. if (ps->force_high)
  1326. cz_dpm_nbdpm_lm_pstate_enable(adev, false);
  1327. else
  1328. cz_dpm_nbdpm_lm_pstate_enable(adev, true);
  1329. }
  1330. return ret;
  1331. }
  1332. /* with dpm enabled */
  1333. static int cz_dpm_set_power_state(struct amdgpu_device *adev)
  1334. {
  1335. int ret = 0;
  1336. cz_dpm_update_sclk_limit(adev);
  1337. cz_dpm_set_deep_sleep_sclk_threshold(adev);
  1338. cz_dpm_set_watermark_threshold(adev);
  1339. cz_dpm_enable_nbdpm(adev);
  1340. cz_dpm_update_low_memory_pstate(adev);
  1341. return ret;
  1342. }
  1343. static void cz_dpm_post_set_power_state(struct amdgpu_device *adev)
  1344. {
  1345. struct cz_power_info *pi = cz_get_pi(adev);
  1346. struct amdgpu_ps *ps = &pi->requested_rps;
  1347. cz_update_current_ps(adev, ps);
  1348. }
  1349. static int cz_dpm_force_highest(struct amdgpu_device *adev)
  1350. {
  1351. struct cz_power_info *pi = cz_get_pi(adev);
  1352. int ret = 0;
  1353. if (pi->sclk_dpm.soft_min_clk != pi->sclk_dpm.soft_max_clk) {
  1354. pi->sclk_dpm.soft_min_clk =
  1355. pi->sclk_dpm.soft_max_clk;
  1356. ret = cz_send_msg_to_smc_with_parameter(adev,
  1357. PPSMC_MSG_SetSclkSoftMin,
  1358. cz_get_sclk_level(adev,
  1359. pi->sclk_dpm.soft_min_clk,
  1360. PPSMC_MSG_SetSclkSoftMin));
  1361. if (ret)
  1362. return ret;
  1363. }
  1364. return ret;
  1365. }
  1366. static int cz_dpm_force_lowest(struct amdgpu_device *adev)
  1367. {
  1368. struct cz_power_info *pi = cz_get_pi(adev);
  1369. int ret = 0;
  1370. if (pi->sclk_dpm.soft_max_clk != pi->sclk_dpm.soft_min_clk) {
  1371. pi->sclk_dpm.soft_max_clk = pi->sclk_dpm.soft_min_clk;
  1372. ret = cz_send_msg_to_smc_with_parameter(adev,
  1373. PPSMC_MSG_SetSclkSoftMax,
  1374. cz_get_sclk_level(adev,
  1375. pi->sclk_dpm.soft_max_clk,
  1376. PPSMC_MSG_SetSclkSoftMax));
  1377. if (ret)
  1378. return ret;
  1379. }
  1380. return ret;
  1381. }
  1382. static uint32_t cz_dpm_get_max_sclk_level(struct amdgpu_device *adev)
  1383. {
  1384. struct cz_power_info *pi = cz_get_pi(adev);
  1385. if (!pi->max_sclk_level) {
  1386. cz_send_msg_to_smc(adev, PPSMC_MSG_GetMaxSclkLevel);
  1387. pi->max_sclk_level = cz_get_argument(adev) + 1;
  1388. }
  1389. if (pi->max_sclk_level > CZ_MAX_HARDWARE_POWERLEVELS) {
  1390. DRM_ERROR("Invalid max sclk level!\n");
  1391. return -EINVAL;
  1392. }
  1393. return pi->max_sclk_level;
  1394. }
  1395. static int cz_dpm_unforce_dpm_levels(struct amdgpu_device *adev)
  1396. {
  1397. struct cz_power_info *pi = cz_get_pi(adev);
  1398. struct amdgpu_clock_voltage_dependency_table *dep_table =
  1399. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  1400. uint32_t level = 0;
  1401. int ret = 0;
  1402. pi->sclk_dpm.soft_min_clk = dep_table->entries[0].clk;
  1403. level = cz_dpm_get_max_sclk_level(adev) - 1;
  1404. if (level < dep_table->count)
  1405. pi->sclk_dpm.soft_max_clk = dep_table->entries[level].clk;
  1406. else
  1407. pi->sclk_dpm.soft_max_clk =
  1408. dep_table->entries[dep_table->count - 1].clk;
  1409. /* get min/max sclk soft value
  1410. * notify SMU to execute */
  1411. ret = cz_send_msg_to_smc_with_parameter(adev,
  1412. PPSMC_MSG_SetSclkSoftMin,
  1413. cz_get_sclk_level(adev,
  1414. pi->sclk_dpm.soft_min_clk,
  1415. PPSMC_MSG_SetSclkSoftMin));
  1416. if (ret)
  1417. return ret;
  1418. ret = cz_send_msg_to_smc_with_parameter(adev,
  1419. PPSMC_MSG_SetSclkSoftMax,
  1420. cz_get_sclk_level(adev,
  1421. pi->sclk_dpm.soft_max_clk,
  1422. PPSMC_MSG_SetSclkSoftMax));
  1423. if (ret)
  1424. return ret;
  1425. DRM_DEBUG("DPM unforce state min=%d, max=%d.\n",
  1426. pi->sclk_dpm.soft_min_clk,
  1427. pi->sclk_dpm.soft_max_clk);
  1428. return 0;
  1429. }
  1430. static int cz_dpm_force_dpm_level(struct amdgpu_device *adev,
  1431. enum amdgpu_dpm_forced_level level)
  1432. {
  1433. int ret = 0;
  1434. switch (level) {
  1435. case AMDGPU_DPM_FORCED_LEVEL_HIGH:
  1436. ret = cz_dpm_unforce_dpm_levels(adev);
  1437. if (ret)
  1438. return ret;
  1439. ret = cz_dpm_force_highest(adev);
  1440. if (ret)
  1441. return ret;
  1442. break;
  1443. case AMDGPU_DPM_FORCED_LEVEL_LOW:
  1444. ret = cz_dpm_unforce_dpm_levels(adev);
  1445. if (ret)
  1446. return ret;
  1447. ret = cz_dpm_force_lowest(adev);
  1448. if (ret)
  1449. return ret;
  1450. break;
  1451. case AMDGPU_DPM_FORCED_LEVEL_AUTO:
  1452. ret = cz_dpm_unforce_dpm_levels(adev);
  1453. if (ret)
  1454. return ret;
  1455. break;
  1456. default:
  1457. break;
  1458. }
  1459. adev->pm.dpm.forced_level = level;
  1460. return ret;
  1461. }
  1462. /* fix me, display configuration change lists here
  1463. * mostly dal related*/
  1464. static void cz_dpm_display_configuration_changed(struct amdgpu_device *adev)
  1465. {
  1466. }
  1467. static uint32_t cz_dpm_get_sclk(struct amdgpu_device *adev, bool low)
  1468. {
  1469. struct cz_power_info *pi = cz_get_pi(adev);
  1470. struct cz_ps *requested_state = cz_get_ps(&pi->requested_rps);
  1471. if (low)
  1472. return requested_state->levels[0].sclk;
  1473. else
  1474. return requested_state->levels[requested_state->num_levels - 1].sclk;
  1475. }
  1476. static uint32_t cz_dpm_get_mclk(struct amdgpu_device *adev, bool low)
  1477. {
  1478. struct cz_power_info *pi = cz_get_pi(adev);
  1479. return pi->sys_info.bootup_uma_clk;
  1480. }
  1481. static int cz_enable_uvd_dpm(struct amdgpu_device *adev, bool enable)
  1482. {
  1483. struct cz_power_info *pi = cz_get_pi(adev);
  1484. int ret = 0;
  1485. if (enable && pi->caps_uvd_dpm ) {
  1486. pi->dpm_flags |= DPMFlags_UVD_Enabled;
  1487. DRM_DEBUG("UVD DPM Enabled.\n");
  1488. ret = cz_send_msg_to_smc_with_parameter(adev,
  1489. PPSMC_MSG_EnableAllSmuFeatures, UVD_DPM_MASK);
  1490. } else {
  1491. pi->dpm_flags &= ~DPMFlags_UVD_Enabled;
  1492. DRM_DEBUG("UVD DPM Stopped\n");
  1493. ret = cz_send_msg_to_smc_with_parameter(adev,
  1494. PPSMC_MSG_DisableAllSmuFeatures, UVD_DPM_MASK);
  1495. }
  1496. return ret;
  1497. }
  1498. static int cz_update_uvd_dpm(struct amdgpu_device *adev, bool gate)
  1499. {
  1500. return cz_enable_uvd_dpm(adev, !gate);
  1501. }
  1502. static void cz_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate)
  1503. {
  1504. struct cz_power_info *pi = cz_get_pi(adev);
  1505. int ret;
  1506. if (pi->uvd_power_gated == gate)
  1507. return;
  1508. pi->uvd_power_gated = gate;
  1509. if (gate) {
  1510. if (pi->caps_uvd_pg) {
  1511. /* disable clockgating so we can properly shut down the block */
  1512. ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1513. AMD_CG_STATE_UNGATE);
  1514. /* shutdown the UVD block */
  1515. ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1516. AMD_PG_STATE_GATE);
  1517. /* XXX: check for errors */
  1518. }
  1519. cz_update_uvd_dpm(adev, gate);
  1520. if (pi->caps_uvd_pg)
  1521. /* power off the UVD block */
  1522. cz_send_msg_to_smc(adev, PPSMC_MSG_UVDPowerOFF);
  1523. } else {
  1524. if (pi->caps_uvd_pg) {
  1525. /* power on the UVD block */
  1526. if (pi->uvd_dynamic_pg)
  1527. cz_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_UVDPowerON, 1);
  1528. else
  1529. cz_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_UVDPowerON, 0);
  1530. /* re-init the UVD block */
  1531. ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1532. AMD_PG_STATE_UNGATE);
  1533. /* enable clockgating. hw will dynamically gate/ungate clocks on the fly */
  1534. ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1535. AMD_CG_STATE_GATE);
  1536. /* XXX: check for errors */
  1537. }
  1538. cz_update_uvd_dpm(adev, gate);
  1539. }
  1540. }
  1541. static int cz_enable_vce_dpm(struct amdgpu_device *adev, bool enable)
  1542. {
  1543. struct cz_power_info *pi = cz_get_pi(adev);
  1544. int ret = 0;
  1545. if (enable && pi->caps_vce_dpm) {
  1546. pi->dpm_flags |= DPMFlags_VCE_Enabled;
  1547. DRM_DEBUG("VCE DPM Enabled.\n");
  1548. ret = cz_send_msg_to_smc_with_parameter(adev,
  1549. PPSMC_MSG_EnableAllSmuFeatures, VCE_DPM_MASK);
  1550. } else {
  1551. pi->dpm_flags &= ~DPMFlags_VCE_Enabled;
  1552. DRM_DEBUG("VCE DPM Stopped\n");
  1553. ret = cz_send_msg_to_smc_with_parameter(adev,
  1554. PPSMC_MSG_DisableAllSmuFeatures, VCE_DPM_MASK);
  1555. }
  1556. return ret;
  1557. }
  1558. static int cz_update_vce_dpm(struct amdgpu_device *adev)
  1559. {
  1560. struct cz_power_info *pi = cz_get_pi(adev);
  1561. struct amdgpu_vce_clock_voltage_dependency_table *table =
  1562. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  1563. /* Stable Pstate is enabled and we need to set the VCE DPM to highest level */
  1564. if (pi->caps_stable_power_state) {
  1565. pi->vce_dpm.hard_min_clk = table->entries[table->count-1].ecclk;
  1566. } else { /* non-stable p-state cases. without vce.Arbiter.EcclkHardMin */
  1567. pi->vce_dpm.hard_min_clk = table->entries[0].ecclk;
  1568. }
  1569. cz_send_msg_to_smc_with_parameter(adev,
  1570. PPSMC_MSG_SetEclkHardMin,
  1571. cz_get_eclk_level(adev,
  1572. pi->vce_dpm.hard_min_clk,
  1573. PPSMC_MSG_SetEclkHardMin));
  1574. return 0;
  1575. }
  1576. static void cz_dpm_powergate_vce(struct amdgpu_device *adev, bool gate)
  1577. {
  1578. struct cz_power_info *pi = cz_get_pi(adev);
  1579. if (pi->caps_vce_pg) {
  1580. if (pi->vce_power_gated != gate) {
  1581. if (gate) {
  1582. /* disable clockgating so we can properly shut down the block */
  1583. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1584. AMD_CG_STATE_UNGATE);
  1585. /* shutdown the VCE block */
  1586. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1587. AMD_PG_STATE_GATE);
  1588. cz_enable_vce_dpm(adev, false);
  1589. /* TODO: to figure out why vce can't be poweroff. */
  1590. /* cz_send_msg_to_smc(adev, PPSMC_MSG_VCEPowerOFF); */
  1591. pi->vce_power_gated = true;
  1592. } else {
  1593. cz_send_msg_to_smc(adev, PPSMC_MSG_VCEPowerON);
  1594. pi->vce_power_gated = false;
  1595. /* re-init the VCE block */
  1596. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1597. AMD_PG_STATE_UNGATE);
  1598. /* enable clockgating. hw will dynamically gate/ungate clocks on the fly */
  1599. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1600. AMD_CG_STATE_GATE);
  1601. cz_update_vce_dpm(adev);
  1602. cz_enable_vce_dpm(adev, true);
  1603. }
  1604. } else {
  1605. if (! pi->vce_power_gated) {
  1606. cz_update_vce_dpm(adev);
  1607. }
  1608. }
  1609. } else { /*pi->caps_vce_pg*/
  1610. cz_update_vce_dpm(adev);
  1611. cz_enable_vce_dpm(adev, !gate);
  1612. }
  1613. }
  1614. const struct amd_ip_funcs cz_dpm_ip_funcs = {
  1615. .early_init = cz_dpm_early_init,
  1616. .late_init = cz_dpm_late_init,
  1617. .sw_init = cz_dpm_sw_init,
  1618. .sw_fini = cz_dpm_sw_fini,
  1619. .hw_init = cz_dpm_hw_init,
  1620. .hw_fini = cz_dpm_hw_fini,
  1621. .suspend = cz_dpm_suspend,
  1622. .resume = cz_dpm_resume,
  1623. .is_idle = NULL,
  1624. .wait_for_idle = NULL,
  1625. .soft_reset = NULL,
  1626. .print_status = NULL,
  1627. .set_clockgating_state = cz_dpm_set_clockgating_state,
  1628. .set_powergating_state = cz_dpm_set_powergating_state,
  1629. };
  1630. static const struct amdgpu_dpm_funcs cz_dpm_funcs = {
  1631. .get_temperature = cz_dpm_get_temperature,
  1632. .pre_set_power_state = cz_dpm_pre_set_power_state,
  1633. .set_power_state = cz_dpm_set_power_state,
  1634. .post_set_power_state = cz_dpm_post_set_power_state,
  1635. .display_configuration_changed = cz_dpm_display_configuration_changed,
  1636. .get_sclk = cz_dpm_get_sclk,
  1637. .get_mclk = cz_dpm_get_mclk,
  1638. .print_power_state = cz_dpm_print_power_state,
  1639. .debugfs_print_current_performance_level =
  1640. cz_dpm_debugfs_print_current_performance_level,
  1641. .force_performance_level = cz_dpm_force_dpm_level,
  1642. .vblank_too_short = NULL,
  1643. .powergate_uvd = cz_dpm_powergate_uvd,
  1644. .powergate_vce = cz_dpm_powergate_vce,
  1645. };
  1646. static void cz_dpm_set_funcs(struct amdgpu_device *adev)
  1647. {
  1648. if (NULL == adev->pm.funcs)
  1649. adev->pm.funcs = &cz_dpm_funcs;
  1650. }