amdgpu_pm.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. /*
  2. * Copyright 2017 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Rafał Miłecki <zajec5@gmail.com>
  23. * Alex Deucher <alexdeucher@gmail.com>
  24. */
  25. #include <drm/drmP.h>
  26. #include "amdgpu.h"
  27. #include "amdgpu_drv.h"
  28. #include "amdgpu_pm.h"
  29. #include "amdgpu_dpm.h"
  30. #include "atom.h"
  31. #include <linux/power_supply.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
  35. static const struct cg_flag_name clocks[] = {
  36. {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
  37. {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
  38. {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
  39. {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
  40. {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
  41. {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
  42. {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
  43. {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
  44. {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
  45. {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
  46. {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
  47. {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
  48. {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
  49. {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
  50. {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
  51. {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
  52. {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
  53. {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
  54. {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
  55. {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
  56. {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
  57. {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
  58. {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
  59. {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
  60. {0, NULL},
  61. };
  62. void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
  63. {
  64. if (adev->pm.dpm_enabled) {
  65. mutex_lock(&adev->pm.mutex);
  66. if (power_supply_is_system_supplied() > 0)
  67. adev->pm.dpm.ac_power = true;
  68. else
  69. adev->pm.dpm.ac_power = false;
  70. if (adev->powerplay.pp_funcs->enable_bapm)
  71. amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
  72. mutex_unlock(&adev->pm.mutex);
  73. }
  74. }
  75. static ssize_t amdgpu_get_dpm_state(struct device *dev,
  76. struct device_attribute *attr,
  77. char *buf)
  78. {
  79. struct drm_device *ddev = dev_get_drvdata(dev);
  80. struct amdgpu_device *adev = ddev->dev_private;
  81. enum amd_pm_state_type pm;
  82. if (adev->powerplay.pp_funcs->get_current_power_state)
  83. pm = amdgpu_dpm_get_current_power_state(adev);
  84. else
  85. pm = adev->pm.dpm.user_state;
  86. return snprintf(buf, PAGE_SIZE, "%s\n",
  87. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  88. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  89. }
  90. static ssize_t amdgpu_set_dpm_state(struct device *dev,
  91. struct device_attribute *attr,
  92. const char *buf,
  93. size_t count)
  94. {
  95. struct drm_device *ddev = dev_get_drvdata(dev);
  96. struct amdgpu_device *adev = ddev->dev_private;
  97. enum amd_pm_state_type state;
  98. if (strncmp("battery", buf, strlen("battery")) == 0)
  99. state = POWER_STATE_TYPE_BATTERY;
  100. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  101. state = POWER_STATE_TYPE_BALANCED;
  102. else if (strncmp("performance", buf, strlen("performance")) == 0)
  103. state = POWER_STATE_TYPE_PERFORMANCE;
  104. else {
  105. count = -EINVAL;
  106. goto fail;
  107. }
  108. if (adev->powerplay.pp_funcs->dispatch_tasks) {
  109. amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
  110. } else {
  111. mutex_lock(&adev->pm.mutex);
  112. adev->pm.dpm.user_state = state;
  113. mutex_unlock(&adev->pm.mutex);
  114. /* Can't set dpm state when the card is off */
  115. if (!(adev->flags & AMD_IS_PX) ||
  116. (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
  117. amdgpu_pm_compute_clocks(adev);
  118. }
  119. fail:
  120. return count;
  121. }
  122. static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
  123. struct device_attribute *attr,
  124. char *buf)
  125. {
  126. struct drm_device *ddev = dev_get_drvdata(dev);
  127. struct amdgpu_device *adev = ddev->dev_private;
  128. enum amd_dpm_forced_level level = 0xff;
  129. if ((adev->flags & AMD_IS_PX) &&
  130. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  131. return snprintf(buf, PAGE_SIZE, "off\n");
  132. if (adev->powerplay.pp_funcs->get_performance_level)
  133. level = amdgpu_dpm_get_performance_level(adev);
  134. else
  135. level = adev->pm.dpm.forced_level;
  136. return snprintf(buf, PAGE_SIZE, "%s\n",
  137. (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  138. (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
  139. (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
  140. (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
  141. (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
  142. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
  143. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
  144. (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
  145. "unknown");
  146. }
  147. static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
  148. struct device_attribute *attr,
  149. const char *buf,
  150. size_t count)
  151. {
  152. struct drm_device *ddev = dev_get_drvdata(dev);
  153. struct amdgpu_device *adev = ddev->dev_private;
  154. enum amd_dpm_forced_level level;
  155. enum amd_dpm_forced_level current_level = 0xff;
  156. int ret = 0;
  157. /* Can't force performance level when the card is off */
  158. if ((adev->flags & AMD_IS_PX) &&
  159. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  160. return -EINVAL;
  161. if (adev->powerplay.pp_funcs->get_performance_level)
  162. current_level = amdgpu_dpm_get_performance_level(adev);
  163. if (strncmp("low", buf, strlen("low")) == 0) {
  164. level = AMD_DPM_FORCED_LEVEL_LOW;
  165. } else if (strncmp("high", buf, strlen("high")) == 0) {
  166. level = AMD_DPM_FORCED_LEVEL_HIGH;
  167. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  168. level = AMD_DPM_FORCED_LEVEL_AUTO;
  169. } else if (strncmp("manual", buf, strlen("manual")) == 0) {
  170. level = AMD_DPM_FORCED_LEVEL_MANUAL;
  171. } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
  172. level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
  173. } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
  174. level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
  175. } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
  176. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
  177. } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
  178. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
  179. } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
  180. level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
  181. } else {
  182. count = -EINVAL;
  183. goto fail;
  184. }
  185. if (current_level == level)
  186. return count;
  187. if (adev->powerplay.pp_funcs->force_performance_level) {
  188. mutex_lock(&adev->pm.mutex);
  189. if (adev->pm.dpm.thermal_active) {
  190. count = -EINVAL;
  191. mutex_unlock(&adev->pm.mutex);
  192. goto fail;
  193. }
  194. ret = amdgpu_dpm_force_performance_level(adev, level);
  195. if (ret)
  196. count = -EINVAL;
  197. else
  198. adev->pm.dpm.forced_level = level;
  199. mutex_unlock(&adev->pm.mutex);
  200. }
  201. fail:
  202. return count;
  203. }
  204. static ssize_t amdgpu_get_pp_num_states(struct device *dev,
  205. struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct drm_device *ddev = dev_get_drvdata(dev);
  209. struct amdgpu_device *adev = ddev->dev_private;
  210. struct pp_states_info data;
  211. int i, buf_len;
  212. if (adev->powerplay.pp_funcs->get_pp_num_states)
  213. amdgpu_dpm_get_pp_num_states(adev, &data);
  214. buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
  215. for (i = 0; i < data.nums; i++)
  216. buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
  217. (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
  218. (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
  219. (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
  220. (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
  221. return buf_len;
  222. }
  223. static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
  224. struct device_attribute *attr,
  225. char *buf)
  226. {
  227. struct drm_device *ddev = dev_get_drvdata(dev);
  228. struct amdgpu_device *adev = ddev->dev_private;
  229. struct pp_states_info data;
  230. enum amd_pm_state_type pm = 0;
  231. int i = 0;
  232. if (adev->powerplay.pp_funcs->get_current_power_state
  233. && adev->powerplay.pp_funcs->get_pp_num_states) {
  234. pm = amdgpu_dpm_get_current_power_state(adev);
  235. amdgpu_dpm_get_pp_num_states(adev, &data);
  236. for (i = 0; i < data.nums; i++) {
  237. if (pm == data.states[i])
  238. break;
  239. }
  240. if (i == data.nums)
  241. i = -EINVAL;
  242. }
  243. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  244. }
  245. static ssize_t amdgpu_get_pp_force_state(struct device *dev,
  246. struct device_attribute *attr,
  247. char *buf)
  248. {
  249. struct drm_device *ddev = dev_get_drvdata(dev);
  250. struct amdgpu_device *adev = ddev->dev_private;
  251. if (adev->pp_force_state_enabled)
  252. return amdgpu_get_pp_cur_state(dev, attr, buf);
  253. else
  254. return snprintf(buf, PAGE_SIZE, "\n");
  255. }
  256. static ssize_t amdgpu_set_pp_force_state(struct device *dev,
  257. struct device_attribute *attr,
  258. const char *buf,
  259. size_t count)
  260. {
  261. struct drm_device *ddev = dev_get_drvdata(dev);
  262. struct amdgpu_device *adev = ddev->dev_private;
  263. enum amd_pm_state_type state = 0;
  264. unsigned long idx;
  265. int ret;
  266. if (strlen(buf) == 1)
  267. adev->pp_force_state_enabled = false;
  268. else if (adev->powerplay.pp_funcs->dispatch_tasks &&
  269. adev->powerplay.pp_funcs->get_pp_num_states) {
  270. struct pp_states_info data;
  271. ret = kstrtoul(buf, 0, &idx);
  272. if (ret || idx >= ARRAY_SIZE(data.states)) {
  273. count = -EINVAL;
  274. goto fail;
  275. }
  276. amdgpu_dpm_get_pp_num_states(adev, &data);
  277. state = data.states[idx];
  278. /* only set user selected power states */
  279. if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
  280. state != POWER_STATE_TYPE_DEFAULT) {
  281. amdgpu_dpm_dispatch_task(adev,
  282. AMD_PP_TASK_ENABLE_USER_STATE, &state);
  283. adev->pp_force_state_enabled = true;
  284. }
  285. }
  286. fail:
  287. return count;
  288. }
  289. static ssize_t amdgpu_get_pp_table(struct device *dev,
  290. struct device_attribute *attr,
  291. char *buf)
  292. {
  293. struct drm_device *ddev = dev_get_drvdata(dev);
  294. struct amdgpu_device *adev = ddev->dev_private;
  295. char *table = NULL;
  296. int size;
  297. if (adev->powerplay.pp_funcs->get_pp_table)
  298. size = amdgpu_dpm_get_pp_table(adev, &table);
  299. else
  300. return 0;
  301. if (size >= PAGE_SIZE)
  302. size = PAGE_SIZE - 1;
  303. memcpy(buf, table, size);
  304. return size;
  305. }
  306. static ssize_t amdgpu_set_pp_table(struct device *dev,
  307. struct device_attribute *attr,
  308. const char *buf,
  309. size_t count)
  310. {
  311. struct drm_device *ddev = dev_get_drvdata(dev);
  312. struct amdgpu_device *adev = ddev->dev_private;
  313. if (adev->powerplay.pp_funcs->set_pp_table)
  314. amdgpu_dpm_set_pp_table(adev, buf, count);
  315. return count;
  316. }
  317. static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
  318. struct device_attribute *attr,
  319. const char *buf,
  320. size_t count)
  321. {
  322. struct drm_device *ddev = dev_get_drvdata(dev);
  323. struct amdgpu_device *adev = ddev->dev_private;
  324. int ret;
  325. uint32_t parameter_size = 0;
  326. long parameter[64];
  327. char buf_cpy[128];
  328. char *tmp_str;
  329. char *sub_str;
  330. const char delimiter[3] = {' ', '\n', '\0'};
  331. uint32_t type;
  332. if (count > 127)
  333. return -EINVAL;
  334. if (*buf == 's')
  335. type = PP_OD_EDIT_SCLK_VDDC_TABLE;
  336. else if (*buf == 'm')
  337. type = PP_OD_EDIT_MCLK_VDDC_TABLE;
  338. else if(*buf == 'r')
  339. type = PP_OD_RESTORE_DEFAULT_TABLE;
  340. else if (*buf == 'c')
  341. type = PP_OD_COMMIT_DPM_TABLE;
  342. else
  343. return -EINVAL;
  344. memcpy(buf_cpy, buf, count+1);
  345. tmp_str = buf_cpy;
  346. while (isspace(*++tmp_str));
  347. while (tmp_str[0]) {
  348. sub_str = strsep(&tmp_str, delimiter);
  349. ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
  350. if (ret)
  351. return -EINVAL;
  352. parameter_size++;
  353. while (isspace(*tmp_str))
  354. tmp_str++;
  355. }
  356. if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
  357. ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
  358. parameter, parameter_size);
  359. if (ret)
  360. return -EINVAL;
  361. if (type == PP_OD_COMMIT_DPM_TABLE) {
  362. if (adev->powerplay.pp_funcs->dispatch_tasks) {
  363. amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
  364. return count;
  365. } else {
  366. return -EINVAL;
  367. }
  368. }
  369. return count;
  370. }
  371. static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
  372. struct device_attribute *attr,
  373. char *buf)
  374. {
  375. struct drm_device *ddev = dev_get_drvdata(dev);
  376. struct amdgpu_device *adev = ddev->dev_private;
  377. uint32_t size = 0;
  378. if (adev->powerplay.pp_funcs->print_clock_levels) {
  379. size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
  380. size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
  381. return size;
  382. } else {
  383. return snprintf(buf, PAGE_SIZE, "\n");
  384. }
  385. }
  386. static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
  387. struct device_attribute *attr,
  388. char *buf)
  389. {
  390. struct drm_device *ddev = dev_get_drvdata(dev);
  391. struct amdgpu_device *adev = ddev->dev_private;
  392. if (adev->powerplay.pp_funcs->print_clock_levels)
  393. return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
  394. else
  395. return snprintf(buf, PAGE_SIZE, "\n");
  396. }
  397. static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
  398. struct device_attribute *attr,
  399. const char *buf,
  400. size_t count)
  401. {
  402. struct drm_device *ddev = dev_get_drvdata(dev);
  403. struct amdgpu_device *adev = ddev->dev_private;
  404. int ret;
  405. long level;
  406. uint32_t i, mask = 0;
  407. char sub_str[2];
  408. for (i = 0; i < strlen(buf); i++) {
  409. if (*(buf + i) == '\n')
  410. continue;
  411. sub_str[0] = *(buf + i);
  412. sub_str[1] = '\0';
  413. ret = kstrtol(sub_str, 0, &level);
  414. if (ret) {
  415. count = -EINVAL;
  416. goto fail;
  417. }
  418. mask |= 1 << level;
  419. }
  420. if (adev->powerplay.pp_funcs->force_clock_level)
  421. amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
  422. fail:
  423. return count;
  424. }
  425. static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
  426. struct device_attribute *attr,
  427. char *buf)
  428. {
  429. struct drm_device *ddev = dev_get_drvdata(dev);
  430. struct amdgpu_device *adev = ddev->dev_private;
  431. if (adev->powerplay.pp_funcs->print_clock_levels)
  432. return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
  433. else
  434. return snprintf(buf, PAGE_SIZE, "\n");
  435. }
  436. static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
  437. struct device_attribute *attr,
  438. const char *buf,
  439. size_t count)
  440. {
  441. struct drm_device *ddev = dev_get_drvdata(dev);
  442. struct amdgpu_device *adev = ddev->dev_private;
  443. int ret;
  444. long level;
  445. uint32_t i, mask = 0;
  446. char sub_str[2];
  447. for (i = 0; i < strlen(buf); i++) {
  448. if (*(buf + i) == '\n')
  449. continue;
  450. sub_str[0] = *(buf + i);
  451. sub_str[1] = '\0';
  452. ret = kstrtol(sub_str, 0, &level);
  453. if (ret) {
  454. count = -EINVAL;
  455. goto fail;
  456. }
  457. mask |= 1 << level;
  458. }
  459. if (adev->powerplay.pp_funcs->force_clock_level)
  460. amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
  461. fail:
  462. return count;
  463. }
  464. static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
  465. struct device_attribute *attr,
  466. char *buf)
  467. {
  468. struct drm_device *ddev = dev_get_drvdata(dev);
  469. struct amdgpu_device *adev = ddev->dev_private;
  470. if (adev->powerplay.pp_funcs->print_clock_levels)
  471. return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
  472. else
  473. return snprintf(buf, PAGE_SIZE, "\n");
  474. }
  475. static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
  476. struct device_attribute *attr,
  477. const char *buf,
  478. size_t count)
  479. {
  480. struct drm_device *ddev = dev_get_drvdata(dev);
  481. struct amdgpu_device *adev = ddev->dev_private;
  482. int ret;
  483. long level;
  484. uint32_t i, mask = 0;
  485. char sub_str[2];
  486. for (i = 0; i < strlen(buf); i++) {
  487. if (*(buf + i) == '\n')
  488. continue;
  489. sub_str[0] = *(buf + i);
  490. sub_str[1] = '\0';
  491. ret = kstrtol(sub_str, 0, &level);
  492. if (ret) {
  493. count = -EINVAL;
  494. goto fail;
  495. }
  496. mask |= 1 << level;
  497. }
  498. if (adev->powerplay.pp_funcs->force_clock_level)
  499. amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
  500. fail:
  501. return count;
  502. }
  503. static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
  504. struct device_attribute *attr,
  505. char *buf)
  506. {
  507. struct drm_device *ddev = dev_get_drvdata(dev);
  508. struct amdgpu_device *adev = ddev->dev_private;
  509. uint32_t value = 0;
  510. if (adev->powerplay.pp_funcs->get_sclk_od)
  511. value = amdgpu_dpm_get_sclk_od(adev);
  512. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  513. }
  514. static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
  515. struct device_attribute *attr,
  516. const char *buf,
  517. size_t count)
  518. {
  519. struct drm_device *ddev = dev_get_drvdata(dev);
  520. struct amdgpu_device *adev = ddev->dev_private;
  521. int ret;
  522. long int value;
  523. ret = kstrtol(buf, 0, &value);
  524. if (ret) {
  525. count = -EINVAL;
  526. goto fail;
  527. }
  528. if (adev->powerplay.pp_funcs->set_sclk_od)
  529. amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
  530. if (adev->powerplay.pp_funcs->dispatch_tasks) {
  531. amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
  532. } else {
  533. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  534. amdgpu_pm_compute_clocks(adev);
  535. }
  536. fail:
  537. return count;
  538. }
  539. static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
  540. struct device_attribute *attr,
  541. char *buf)
  542. {
  543. struct drm_device *ddev = dev_get_drvdata(dev);
  544. struct amdgpu_device *adev = ddev->dev_private;
  545. uint32_t value = 0;
  546. if (adev->powerplay.pp_funcs->get_mclk_od)
  547. value = amdgpu_dpm_get_mclk_od(adev);
  548. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  549. }
  550. static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
  551. struct device_attribute *attr,
  552. const char *buf,
  553. size_t count)
  554. {
  555. struct drm_device *ddev = dev_get_drvdata(dev);
  556. struct amdgpu_device *adev = ddev->dev_private;
  557. int ret;
  558. long int value;
  559. ret = kstrtol(buf, 0, &value);
  560. if (ret) {
  561. count = -EINVAL;
  562. goto fail;
  563. }
  564. if (adev->powerplay.pp_funcs->set_mclk_od)
  565. amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
  566. if (adev->powerplay.pp_funcs->dispatch_tasks) {
  567. amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
  568. } else {
  569. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  570. amdgpu_pm_compute_clocks(adev);
  571. }
  572. fail:
  573. return count;
  574. }
  575. static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
  576. struct device_attribute *attr,
  577. char *buf)
  578. {
  579. struct drm_device *ddev = dev_get_drvdata(dev);
  580. struct amdgpu_device *adev = ddev->dev_private;
  581. if (adev->powerplay.pp_funcs->get_power_profile_mode)
  582. return amdgpu_dpm_get_power_profile_mode(adev, buf);
  583. return snprintf(buf, PAGE_SIZE, "\n");
  584. }
  585. static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
  586. struct device_attribute *attr,
  587. const char *buf,
  588. size_t count)
  589. {
  590. int ret = 0xff;
  591. struct drm_device *ddev = dev_get_drvdata(dev);
  592. struct amdgpu_device *adev = ddev->dev_private;
  593. uint32_t parameter_size = 0;
  594. long parameter[64];
  595. char *sub_str, buf_cpy[128];
  596. char *tmp_str;
  597. uint32_t i = 0;
  598. char tmp[2];
  599. long int profile_mode = 0;
  600. const char delimiter[3] = {' ', '\n', '\0'};
  601. tmp[0] = *(buf);
  602. tmp[1] = '\0';
  603. ret = kstrtol(tmp, 0, &profile_mode);
  604. if (ret)
  605. goto fail;
  606. if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
  607. if (count < 2 || count > 127)
  608. return -EINVAL;
  609. while (isspace(*++buf))
  610. i++;
  611. memcpy(buf_cpy, buf, count-i);
  612. tmp_str = buf_cpy;
  613. while (tmp_str[0]) {
  614. sub_str = strsep(&tmp_str, delimiter);
  615. ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
  616. if (ret) {
  617. count = -EINVAL;
  618. goto fail;
  619. }
  620. pr_info("value is %ld \n", parameter[parameter_size]);
  621. parameter_size++;
  622. while (isspace(*tmp_str))
  623. tmp_str++;
  624. }
  625. }
  626. parameter[parameter_size] = profile_mode;
  627. if (adev->powerplay.pp_funcs->set_power_profile_mode)
  628. ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
  629. if (!ret)
  630. return count;
  631. fail:
  632. return -EINVAL;
  633. }
  634. static ssize_t amdgpu_get_pp_power_profile(struct device *dev,
  635. char *buf, struct amd_pp_profile *query)
  636. {
  637. struct drm_device *ddev = dev_get_drvdata(dev);
  638. struct amdgpu_device *adev = ddev->dev_private;
  639. int ret = 0xff;
  640. if (adev->powerplay.pp_funcs->get_power_profile_state)
  641. ret = amdgpu_dpm_get_power_profile_state(
  642. adev, query);
  643. if (ret)
  644. return ret;
  645. return snprintf(buf, PAGE_SIZE,
  646. "%d %d %d %d %d\n",
  647. query->min_sclk / 100,
  648. query->min_mclk / 100,
  649. query->activity_threshold,
  650. query->up_hyst,
  651. query->down_hyst);
  652. }
  653. static ssize_t amdgpu_get_pp_gfx_power_profile(struct device *dev,
  654. struct device_attribute *attr,
  655. char *buf)
  656. {
  657. struct amd_pp_profile query = {0};
  658. query.type = AMD_PP_GFX_PROFILE;
  659. return amdgpu_get_pp_power_profile(dev, buf, &query);
  660. }
  661. static ssize_t amdgpu_get_pp_compute_power_profile(struct device *dev,
  662. struct device_attribute *attr,
  663. char *buf)
  664. {
  665. struct amd_pp_profile query = {0};
  666. query.type = AMD_PP_COMPUTE_PROFILE;
  667. return amdgpu_get_pp_power_profile(dev, buf, &query);
  668. }
  669. static ssize_t amdgpu_set_pp_power_profile(struct device *dev,
  670. const char *buf,
  671. size_t count,
  672. struct amd_pp_profile *request)
  673. {
  674. struct drm_device *ddev = dev_get_drvdata(dev);
  675. struct amdgpu_device *adev = ddev->dev_private;
  676. uint32_t loop = 0;
  677. char *sub_str, buf_cpy[128], *tmp_str;
  678. const char delimiter[3] = {' ', '\n', '\0'};
  679. long int value;
  680. int ret = 0xff;
  681. if (strncmp("reset", buf, strlen("reset")) == 0) {
  682. if (adev->powerplay.pp_funcs->reset_power_profile_state)
  683. ret = amdgpu_dpm_reset_power_profile_state(
  684. adev, request);
  685. if (ret) {
  686. count = -EINVAL;
  687. goto fail;
  688. }
  689. return count;
  690. }
  691. if (strncmp("set", buf, strlen("set")) == 0) {
  692. if (adev->powerplay.pp_funcs->set_power_profile_state)
  693. ret = amdgpu_dpm_set_power_profile_state(
  694. adev, request);
  695. if (ret) {
  696. count = -EINVAL;
  697. goto fail;
  698. }
  699. return count;
  700. }
  701. if (count + 1 >= 128) {
  702. count = -EINVAL;
  703. goto fail;
  704. }
  705. memcpy(buf_cpy, buf, count + 1);
  706. tmp_str = buf_cpy;
  707. while (tmp_str[0]) {
  708. sub_str = strsep(&tmp_str, delimiter);
  709. ret = kstrtol(sub_str, 0, &value);
  710. if (ret) {
  711. count = -EINVAL;
  712. goto fail;
  713. }
  714. switch (loop) {
  715. case 0:
  716. /* input unit MHz convert to dpm table unit 10KHz*/
  717. request->min_sclk = (uint32_t)value * 100;
  718. break;
  719. case 1:
  720. /* input unit MHz convert to dpm table unit 10KHz*/
  721. request->min_mclk = (uint32_t)value * 100;
  722. break;
  723. case 2:
  724. request->activity_threshold = (uint16_t)value;
  725. break;
  726. case 3:
  727. request->up_hyst = (uint8_t)value;
  728. break;
  729. case 4:
  730. request->down_hyst = (uint8_t)value;
  731. break;
  732. default:
  733. break;
  734. }
  735. loop++;
  736. }
  737. if (adev->powerplay.pp_funcs->set_power_profile_state)
  738. ret = amdgpu_dpm_set_power_profile_state(adev, request);
  739. if (ret)
  740. count = -EINVAL;
  741. fail:
  742. return count;
  743. }
  744. static ssize_t amdgpu_set_pp_gfx_power_profile(struct device *dev,
  745. struct device_attribute *attr,
  746. const char *buf,
  747. size_t count)
  748. {
  749. struct amd_pp_profile request = {0};
  750. request.type = AMD_PP_GFX_PROFILE;
  751. return amdgpu_set_pp_power_profile(dev, buf, count, &request);
  752. }
  753. static ssize_t amdgpu_set_pp_compute_power_profile(struct device *dev,
  754. struct device_attribute *attr,
  755. const char *buf,
  756. size_t count)
  757. {
  758. struct amd_pp_profile request = {0};
  759. request.type = AMD_PP_COMPUTE_PROFILE;
  760. return amdgpu_set_pp_power_profile(dev, buf, count, &request);
  761. }
  762. static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
  763. static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
  764. amdgpu_get_dpm_forced_performance_level,
  765. amdgpu_set_dpm_forced_performance_level);
  766. static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
  767. static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
  768. static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
  769. amdgpu_get_pp_force_state,
  770. amdgpu_set_pp_force_state);
  771. static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
  772. amdgpu_get_pp_table,
  773. amdgpu_set_pp_table);
  774. static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
  775. amdgpu_get_pp_dpm_sclk,
  776. amdgpu_set_pp_dpm_sclk);
  777. static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
  778. amdgpu_get_pp_dpm_mclk,
  779. amdgpu_set_pp_dpm_mclk);
  780. static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
  781. amdgpu_get_pp_dpm_pcie,
  782. amdgpu_set_pp_dpm_pcie);
  783. static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
  784. amdgpu_get_pp_sclk_od,
  785. amdgpu_set_pp_sclk_od);
  786. static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
  787. amdgpu_get_pp_mclk_od,
  788. amdgpu_set_pp_mclk_od);
  789. static DEVICE_ATTR(pp_gfx_power_profile, S_IRUGO | S_IWUSR,
  790. amdgpu_get_pp_gfx_power_profile,
  791. amdgpu_set_pp_gfx_power_profile);
  792. static DEVICE_ATTR(pp_compute_power_profile, S_IRUGO | S_IWUSR,
  793. amdgpu_get_pp_compute_power_profile,
  794. amdgpu_set_pp_compute_power_profile);
  795. static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
  796. amdgpu_get_pp_power_profile_mode,
  797. amdgpu_set_pp_power_profile_mode);
  798. static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
  799. amdgpu_get_pp_od_clk_voltage,
  800. amdgpu_set_pp_od_clk_voltage);
  801. static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
  802. struct device_attribute *attr,
  803. char *buf)
  804. {
  805. struct amdgpu_device *adev = dev_get_drvdata(dev);
  806. struct drm_device *ddev = adev->ddev;
  807. int temp;
  808. /* Can't get temperature when the card is off */
  809. if ((adev->flags & AMD_IS_PX) &&
  810. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  811. return -EINVAL;
  812. if (!adev->powerplay.pp_funcs->get_temperature)
  813. temp = 0;
  814. else
  815. temp = amdgpu_dpm_get_temperature(adev);
  816. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  817. }
  818. static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
  819. struct device_attribute *attr,
  820. char *buf)
  821. {
  822. struct amdgpu_device *adev = dev_get_drvdata(dev);
  823. int hyst = to_sensor_dev_attr(attr)->index;
  824. int temp;
  825. if (hyst)
  826. temp = adev->pm.dpm.thermal.min_temp;
  827. else
  828. temp = adev->pm.dpm.thermal.max_temp;
  829. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  830. }
  831. static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
  832. struct device_attribute *attr,
  833. char *buf)
  834. {
  835. struct amdgpu_device *adev = dev_get_drvdata(dev);
  836. u32 pwm_mode = 0;
  837. if (!adev->powerplay.pp_funcs->get_fan_control_mode)
  838. return -EINVAL;
  839. pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
  840. return sprintf(buf, "%i\n", pwm_mode);
  841. }
  842. static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
  843. struct device_attribute *attr,
  844. const char *buf,
  845. size_t count)
  846. {
  847. struct amdgpu_device *adev = dev_get_drvdata(dev);
  848. int err;
  849. int value;
  850. /* Can't adjust fan when the card is off */
  851. if ((adev->flags & AMD_IS_PX) &&
  852. (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  853. return -EINVAL;
  854. if (!adev->powerplay.pp_funcs->set_fan_control_mode)
  855. return -EINVAL;
  856. err = kstrtoint(buf, 10, &value);
  857. if (err)
  858. return err;
  859. amdgpu_dpm_set_fan_control_mode(adev, value);
  860. return count;
  861. }
  862. static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
  863. struct device_attribute *attr,
  864. char *buf)
  865. {
  866. return sprintf(buf, "%i\n", 0);
  867. }
  868. static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
  869. struct device_attribute *attr,
  870. char *buf)
  871. {
  872. return sprintf(buf, "%i\n", 255);
  873. }
  874. static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
  875. struct device_attribute *attr,
  876. const char *buf, size_t count)
  877. {
  878. struct amdgpu_device *adev = dev_get_drvdata(dev);
  879. int err;
  880. u32 value;
  881. /* Can't adjust fan when the card is off */
  882. if ((adev->flags & AMD_IS_PX) &&
  883. (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  884. return -EINVAL;
  885. err = kstrtou32(buf, 10, &value);
  886. if (err)
  887. return err;
  888. value = (value * 100) / 255;
  889. if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
  890. err = amdgpu_dpm_set_fan_speed_percent(adev, value);
  891. if (err)
  892. return err;
  893. }
  894. return count;
  895. }
  896. static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
  897. struct device_attribute *attr,
  898. char *buf)
  899. {
  900. struct amdgpu_device *adev = dev_get_drvdata(dev);
  901. int err;
  902. u32 speed = 0;
  903. /* Can't adjust fan when the card is off */
  904. if ((adev->flags & AMD_IS_PX) &&
  905. (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  906. return -EINVAL;
  907. if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
  908. err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
  909. if (err)
  910. return err;
  911. }
  912. speed = (speed * 255) / 100;
  913. return sprintf(buf, "%i\n", speed);
  914. }
  915. static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
  916. struct device_attribute *attr,
  917. char *buf)
  918. {
  919. struct amdgpu_device *adev = dev_get_drvdata(dev);
  920. int err;
  921. u32 speed = 0;
  922. /* Can't adjust fan when the card is off */
  923. if ((adev->flags & AMD_IS_PX) &&
  924. (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  925. return -EINVAL;
  926. if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
  927. err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
  928. if (err)
  929. return err;
  930. }
  931. return sprintf(buf, "%i\n", speed);
  932. }
  933. static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
  934. struct device_attribute *attr,
  935. char *buf)
  936. {
  937. struct amdgpu_device *adev = dev_get_drvdata(dev);
  938. struct drm_device *ddev = adev->ddev;
  939. u32 vddgfx;
  940. int r, size = sizeof(vddgfx);
  941. /* Can't get voltage when the card is off */
  942. if ((adev->flags & AMD_IS_PX) &&
  943. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  944. return -EINVAL;
  945. /* sanity check PP is enabled */
  946. if (!(adev->powerplay.pp_funcs &&
  947. adev->powerplay.pp_funcs->read_sensor))
  948. return -EINVAL;
  949. /* get the voltage */
  950. r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
  951. (void *)&vddgfx, &size);
  952. if (r)
  953. return r;
  954. return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
  955. }
  956. static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
  957. struct device_attribute *attr,
  958. char *buf)
  959. {
  960. return snprintf(buf, PAGE_SIZE, "vddgfx\n");
  961. }
  962. static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
  963. struct device_attribute *attr,
  964. char *buf)
  965. {
  966. struct amdgpu_device *adev = dev_get_drvdata(dev);
  967. struct drm_device *ddev = adev->ddev;
  968. u32 vddnb;
  969. int r, size = sizeof(vddnb);
  970. /* only APUs have vddnb */
  971. if (adev->flags & AMD_IS_APU)
  972. return -EINVAL;
  973. /* Can't get voltage when the card is off */
  974. if ((adev->flags & AMD_IS_PX) &&
  975. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  976. return -EINVAL;
  977. /* sanity check PP is enabled */
  978. if (!(adev->powerplay.pp_funcs &&
  979. adev->powerplay.pp_funcs->read_sensor))
  980. return -EINVAL;
  981. /* get the voltage */
  982. r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
  983. (void *)&vddnb, &size);
  984. if (r)
  985. return r;
  986. return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
  987. }
  988. static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
  989. struct device_attribute *attr,
  990. char *buf)
  991. {
  992. return snprintf(buf, PAGE_SIZE, "vddnb\n");
  993. }
  994. static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
  995. struct device_attribute *attr,
  996. char *buf)
  997. {
  998. struct amdgpu_device *adev = dev_get_drvdata(dev);
  999. struct drm_device *ddev = adev->ddev;
  1000. struct pp_gpu_power query = {0};
  1001. int r, size = sizeof(query);
  1002. unsigned uw;
  1003. /* Can't get power when the card is off */
  1004. if ((adev->flags & AMD_IS_PX) &&
  1005. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  1006. return -EINVAL;
  1007. /* sanity check PP is enabled */
  1008. if (!(adev->powerplay.pp_funcs &&
  1009. adev->powerplay.pp_funcs->read_sensor))
  1010. return -EINVAL;
  1011. /* get the voltage */
  1012. r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
  1013. (void *)&query, &size);
  1014. if (r)
  1015. return r;
  1016. /* convert to microwatts */
  1017. uw = (query.average_gpu_power >> 8) * 1000000;
  1018. return snprintf(buf, PAGE_SIZE, "%u\n", uw);
  1019. }
  1020. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
  1021. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
  1022. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
  1023. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
  1024. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
  1025. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
  1026. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
  1027. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
  1028. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
  1029. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
  1030. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
  1031. static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
  1032. static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
  1033. static struct attribute *hwmon_attributes[] = {
  1034. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1035. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  1036. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  1037. &sensor_dev_attr_pwm1.dev_attr.attr,
  1038. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1039. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  1040. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  1041. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1042. &sensor_dev_attr_in0_input.dev_attr.attr,
  1043. &sensor_dev_attr_in0_label.dev_attr.attr,
  1044. &sensor_dev_attr_in1_input.dev_attr.attr,
  1045. &sensor_dev_attr_in1_label.dev_attr.attr,
  1046. &sensor_dev_attr_power1_average.dev_attr.attr,
  1047. NULL
  1048. };
  1049. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  1050. struct attribute *attr, int index)
  1051. {
  1052. struct device *dev = kobj_to_dev(kobj);
  1053. struct amdgpu_device *adev = dev_get_drvdata(dev);
  1054. umode_t effective_mode = attr->mode;
  1055. /* no skipping for powerplay */
  1056. if (adev->powerplay.cgs_device)
  1057. return effective_mode;
  1058. /* Skip limit attributes if DPM is not enabled */
  1059. if (!adev->pm.dpm_enabled &&
  1060. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  1061. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
  1062. attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  1063. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  1064. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  1065. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  1066. return 0;
  1067. /* Skip fan attributes if fan is not present */
  1068. if (adev->pm.no_fan &&
  1069. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  1070. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  1071. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  1072. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  1073. return 0;
  1074. /* mask fan attributes if we have no bindings for this asic to expose */
  1075. if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
  1076. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  1077. (!adev->powerplay.pp_funcs->get_fan_control_mode &&
  1078. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  1079. effective_mode &= ~S_IRUGO;
  1080. if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
  1081. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  1082. (!adev->powerplay.pp_funcs->set_fan_control_mode &&
  1083. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  1084. effective_mode &= ~S_IWUSR;
  1085. /* hide max/min values if we can't both query and manage the fan */
  1086. if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
  1087. !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
  1088. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  1089. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  1090. return 0;
  1091. /* requires powerplay */
  1092. if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
  1093. return 0;
  1094. return effective_mode;
  1095. }
  1096. static const struct attribute_group hwmon_attrgroup = {
  1097. .attrs = hwmon_attributes,
  1098. .is_visible = hwmon_attributes_visible,
  1099. };
  1100. static const struct attribute_group *hwmon_groups[] = {
  1101. &hwmon_attrgroup,
  1102. NULL
  1103. };
  1104. void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
  1105. {
  1106. struct amdgpu_device *adev =
  1107. container_of(work, struct amdgpu_device,
  1108. pm.dpm.thermal.work);
  1109. /* switch to the thermal state */
  1110. enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  1111. if (!adev->pm.dpm_enabled)
  1112. return;
  1113. if (adev->powerplay.pp_funcs->get_temperature) {
  1114. int temp = amdgpu_dpm_get_temperature(adev);
  1115. if (temp < adev->pm.dpm.thermal.min_temp)
  1116. /* switch back the user state */
  1117. dpm_state = adev->pm.dpm.user_state;
  1118. } else {
  1119. if (adev->pm.dpm.thermal.high_to_low)
  1120. /* switch back the user state */
  1121. dpm_state = adev->pm.dpm.user_state;
  1122. }
  1123. mutex_lock(&adev->pm.mutex);
  1124. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  1125. adev->pm.dpm.thermal_active = true;
  1126. else
  1127. adev->pm.dpm.thermal_active = false;
  1128. adev->pm.dpm.state = dpm_state;
  1129. mutex_unlock(&adev->pm.mutex);
  1130. amdgpu_pm_compute_clocks(adev);
  1131. }
  1132. static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
  1133. enum amd_pm_state_type dpm_state)
  1134. {
  1135. int i;
  1136. struct amdgpu_ps *ps;
  1137. u32 ui_class;
  1138. bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
  1139. true : false;
  1140. /* check if the vblank period is too short to adjust the mclk */
  1141. if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
  1142. if (amdgpu_dpm_vblank_too_short(adev))
  1143. single_display = false;
  1144. }
  1145. /* certain older asics have a separare 3D performance state,
  1146. * so try that first if the user selected performance
  1147. */
  1148. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  1149. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  1150. /* balanced states don't exist at the moment */
  1151. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  1152. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  1153. restart_search:
  1154. /* Pick the best power state based on current conditions */
  1155. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  1156. ps = &adev->pm.dpm.ps[i];
  1157. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  1158. switch (dpm_state) {
  1159. /* user states */
  1160. case POWER_STATE_TYPE_BATTERY:
  1161. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  1162. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  1163. if (single_display)
  1164. return ps;
  1165. } else
  1166. return ps;
  1167. }
  1168. break;
  1169. case POWER_STATE_TYPE_BALANCED:
  1170. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  1171. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  1172. if (single_display)
  1173. return ps;
  1174. } else
  1175. return ps;
  1176. }
  1177. break;
  1178. case POWER_STATE_TYPE_PERFORMANCE:
  1179. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  1180. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  1181. if (single_display)
  1182. return ps;
  1183. } else
  1184. return ps;
  1185. }
  1186. break;
  1187. /* internal states */
  1188. case POWER_STATE_TYPE_INTERNAL_UVD:
  1189. if (adev->pm.dpm.uvd_ps)
  1190. return adev->pm.dpm.uvd_ps;
  1191. else
  1192. break;
  1193. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  1194. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  1195. return ps;
  1196. break;
  1197. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  1198. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  1199. return ps;
  1200. break;
  1201. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  1202. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  1203. return ps;
  1204. break;
  1205. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  1206. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  1207. return ps;
  1208. break;
  1209. case POWER_STATE_TYPE_INTERNAL_BOOT:
  1210. return adev->pm.dpm.boot_ps;
  1211. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  1212. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  1213. return ps;
  1214. break;
  1215. case POWER_STATE_TYPE_INTERNAL_ACPI:
  1216. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  1217. return ps;
  1218. break;
  1219. case POWER_STATE_TYPE_INTERNAL_ULV:
  1220. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  1221. return ps;
  1222. break;
  1223. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  1224. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  1225. return ps;
  1226. break;
  1227. default:
  1228. break;
  1229. }
  1230. }
  1231. /* use a fallback state if we didn't match */
  1232. switch (dpm_state) {
  1233. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  1234. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  1235. goto restart_search;
  1236. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  1237. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  1238. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  1239. if (adev->pm.dpm.uvd_ps) {
  1240. return adev->pm.dpm.uvd_ps;
  1241. } else {
  1242. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  1243. goto restart_search;
  1244. }
  1245. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  1246. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  1247. goto restart_search;
  1248. case POWER_STATE_TYPE_INTERNAL_ACPI:
  1249. dpm_state = POWER_STATE_TYPE_BATTERY;
  1250. goto restart_search;
  1251. case POWER_STATE_TYPE_BATTERY:
  1252. case POWER_STATE_TYPE_BALANCED:
  1253. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  1254. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  1255. goto restart_search;
  1256. default:
  1257. break;
  1258. }
  1259. return NULL;
  1260. }
  1261. static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
  1262. {
  1263. struct amdgpu_ps *ps;
  1264. enum amd_pm_state_type dpm_state;
  1265. int ret;
  1266. bool equal = false;
  1267. /* if dpm init failed */
  1268. if (!adev->pm.dpm_enabled)
  1269. return;
  1270. if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
  1271. /* add other state override checks here */
  1272. if ((!adev->pm.dpm.thermal_active) &&
  1273. (!adev->pm.dpm.uvd_active))
  1274. adev->pm.dpm.state = adev->pm.dpm.user_state;
  1275. }
  1276. dpm_state = adev->pm.dpm.state;
  1277. ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
  1278. if (ps)
  1279. adev->pm.dpm.requested_ps = ps;
  1280. else
  1281. return;
  1282. if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
  1283. printk("switching from power state:\n");
  1284. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
  1285. printk("switching to power state:\n");
  1286. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
  1287. }
  1288. /* update whether vce is active */
  1289. ps->vce_active = adev->pm.dpm.vce_active;
  1290. if (adev->powerplay.pp_funcs->display_configuration_changed)
  1291. amdgpu_dpm_display_configuration_changed(adev);
  1292. ret = amdgpu_dpm_pre_set_power_state(adev);
  1293. if (ret)
  1294. return;
  1295. if (adev->powerplay.pp_funcs->check_state_equal) {
  1296. if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
  1297. equal = false;
  1298. }
  1299. if (equal)
  1300. return;
  1301. amdgpu_dpm_set_power_state(adev);
  1302. amdgpu_dpm_post_set_power_state(adev);
  1303. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  1304. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  1305. if (adev->powerplay.pp_funcs->force_performance_level) {
  1306. if (adev->pm.dpm.thermal_active) {
  1307. enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
  1308. /* force low perf level for thermal */
  1309. amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
  1310. /* save the user's level */
  1311. adev->pm.dpm.forced_level = level;
  1312. } else {
  1313. /* otherwise, user selected level */
  1314. amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
  1315. }
  1316. }
  1317. }
  1318. void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
  1319. {
  1320. if (adev->powerplay.pp_funcs->powergate_uvd) {
  1321. /* enable/disable UVD */
  1322. mutex_lock(&adev->pm.mutex);
  1323. amdgpu_dpm_powergate_uvd(adev, !enable);
  1324. mutex_unlock(&adev->pm.mutex);
  1325. } else {
  1326. if (enable) {
  1327. mutex_lock(&adev->pm.mutex);
  1328. adev->pm.dpm.uvd_active = true;
  1329. adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
  1330. mutex_unlock(&adev->pm.mutex);
  1331. } else {
  1332. mutex_lock(&adev->pm.mutex);
  1333. adev->pm.dpm.uvd_active = false;
  1334. mutex_unlock(&adev->pm.mutex);
  1335. }
  1336. amdgpu_pm_compute_clocks(adev);
  1337. }
  1338. }
  1339. void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
  1340. {
  1341. if (adev->powerplay.pp_funcs->powergate_vce) {
  1342. /* enable/disable VCE */
  1343. mutex_lock(&adev->pm.mutex);
  1344. amdgpu_dpm_powergate_vce(adev, !enable);
  1345. mutex_unlock(&adev->pm.mutex);
  1346. } else {
  1347. if (enable) {
  1348. mutex_lock(&adev->pm.mutex);
  1349. adev->pm.dpm.vce_active = true;
  1350. /* XXX select vce level based on ring/task */
  1351. adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
  1352. mutex_unlock(&adev->pm.mutex);
  1353. amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1354. AMD_CG_STATE_UNGATE);
  1355. amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1356. AMD_PG_STATE_UNGATE);
  1357. amdgpu_pm_compute_clocks(adev);
  1358. } else {
  1359. amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1360. AMD_PG_STATE_GATE);
  1361. amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1362. AMD_CG_STATE_GATE);
  1363. mutex_lock(&adev->pm.mutex);
  1364. adev->pm.dpm.vce_active = false;
  1365. mutex_unlock(&adev->pm.mutex);
  1366. amdgpu_pm_compute_clocks(adev);
  1367. }
  1368. }
  1369. }
  1370. void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
  1371. {
  1372. int i;
  1373. if (adev->powerplay.pp_funcs->print_power_state == NULL)
  1374. return;
  1375. for (i = 0; i < adev->pm.dpm.num_ps; i++)
  1376. amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
  1377. }
  1378. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  1379. {
  1380. int ret;
  1381. if (adev->pm.sysfs_initialized)
  1382. return 0;
  1383. if (adev->pm.dpm_enabled == 0)
  1384. return 0;
  1385. if (adev->powerplay.pp_funcs->get_temperature == NULL)
  1386. return 0;
  1387. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  1388. DRIVER_NAME, adev,
  1389. hwmon_groups);
  1390. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  1391. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  1392. dev_err(adev->dev,
  1393. "Unable to register hwmon device: %d\n", ret);
  1394. return ret;
  1395. }
  1396. ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
  1397. if (ret) {
  1398. DRM_ERROR("failed to create device file for dpm state\n");
  1399. return ret;
  1400. }
  1401. ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1402. if (ret) {
  1403. DRM_ERROR("failed to create device file for dpm state\n");
  1404. return ret;
  1405. }
  1406. ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
  1407. if (ret) {
  1408. DRM_ERROR("failed to create device file pp_num_states\n");
  1409. return ret;
  1410. }
  1411. ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
  1412. if (ret) {
  1413. DRM_ERROR("failed to create device file pp_cur_state\n");
  1414. return ret;
  1415. }
  1416. ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
  1417. if (ret) {
  1418. DRM_ERROR("failed to create device file pp_force_state\n");
  1419. return ret;
  1420. }
  1421. ret = device_create_file(adev->dev, &dev_attr_pp_table);
  1422. if (ret) {
  1423. DRM_ERROR("failed to create device file pp_table\n");
  1424. return ret;
  1425. }
  1426. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1427. if (ret) {
  1428. DRM_ERROR("failed to create device file pp_dpm_sclk\n");
  1429. return ret;
  1430. }
  1431. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1432. if (ret) {
  1433. DRM_ERROR("failed to create device file pp_dpm_mclk\n");
  1434. return ret;
  1435. }
  1436. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1437. if (ret) {
  1438. DRM_ERROR("failed to create device file pp_dpm_pcie\n");
  1439. return ret;
  1440. }
  1441. ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
  1442. if (ret) {
  1443. DRM_ERROR("failed to create device file pp_sclk_od\n");
  1444. return ret;
  1445. }
  1446. ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
  1447. if (ret) {
  1448. DRM_ERROR("failed to create device file pp_mclk_od\n");
  1449. return ret;
  1450. }
  1451. ret = device_create_file(adev->dev,
  1452. &dev_attr_pp_gfx_power_profile);
  1453. if (ret) {
  1454. DRM_ERROR("failed to create device file "
  1455. "pp_gfx_power_profile\n");
  1456. return ret;
  1457. }
  1458. ret = device_create_file(adev->dev,
  1459. &dev_attr_pp_compute_power_profile);
  1460. if (ret) {
  1461. DRM_ERROR("failed to create device file "
  1462. "pp_compute_power_profile\n");
  1463. return ret;
  1464. }
  1465. ret = device_create_file(adev->dev,
  1466. &dev_attr_pp_power_profile_mode);
  1467. if (ret) {
  1468. DRM_ERROR("failed to create device file "
  1469. "pp_power_profile_mode\n");
  1470. return ret;
  1471. }
  1472. ret = device_create_file(adev->dev,
  1473. &dev_attr_pp_od_clk_voltage);
  1474. if (ret) {
  1475. DRM_ERROR("failed to create device file "
  1476. "pp_od_clk_voltage\n");
  1477. return ret;
  1478. }
  1479. ret = amdgpu_debugfs_pm_init(adev);
  1480. if (ret) {
  1481. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  1482. return ret;
  1483. }
  1484. adev->pm.sysfs_initialized = true;
  1485. return 0;
  1486. }
  1487. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  1488. {
  1489. if (adev->pm.dpm_enabled == 0)
  1490. return;
  1491. if (adev->pm.int_hwmon_dev)
  1492. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  1493. device_remove_file(adev->dev, &dev_attr_power_dpm_state);
  1494. device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1495. device_remove_file(adev->dev, &dev_attr_pp_num_states);
  1496. device_remove_file(adev->dev, &dev_attr_pp_cur_state);
  1497. device_remove_file(adev->dev, &dev_attr_pp_force_state);
  1498. device_remove_file(adev->dev, &dev_attr_pp_table);
  1499. device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1500. device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1501. device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1502. device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
  1503. device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
  1504. device_remove_file(adev->dev,
  1505. &dev_attr_pp_gfx_power_profile);
  1506. device_remove_file(adev->dev,
  1507. &dev_attr_pp_compute_power_profile);
  1508. device_remove_file(adev->dev,
  1509. &dev_attr_pp_power_profile_mode);
  1510. device_remove_file(adev->dev,
  1511. &dev_attr_pp_od_clk_voltage);
  1512. }
  1513. void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
  1514. {
  1515. struct drm_device *ddev = adev->ddev;
  1516. struct drm_crtc *crtc;
  1517. struct amdgpu_crtc *amdgpu_crtc;
  1518. int i = 0;
  1519. if (!adev->pm.dpm_enabled)
  1520. return;
  1521. if (adev->mode_info.num_crtc)
  1522. amdgpu_display_bandwidth_update(adev);
  1523. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  1524. struct amdgpu_ring *ring = adev->rings[i];
  1525. if (ring && ring->ready)
  1526. amdgpu_fence_wait_empty(ring);
  1527. }
  1528. if (adev->powerplay.pp_funcs->dispatch_tasks) {
  1529. amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
  1530. } else {
  1531. mutex_lock(&adev->pm.mutex);
  1532. adev->pm.dpm.new_active_crtcs = 0;
  1533. adev->pm.dpm.new_active_crtc_count = 0;
  1534. if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
  1535. list_for_each_entry(crtc,
  1536. &ddev->mode_config.crtc_list, head) {
  1537. amdgpu_crtc = to_amdgpu_crtc(crtc);
  1538. if (amdgpu_crtc->enabled) {
  1539. adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
  1540. adev->pm.dpm.new_active_crtc_count++;
  1541. }
  1542. }
  1543. }
  1544. /* update battery/ac status */
  1545. if (power_supply_is_system_supplied() > 0)
  1546. adev->pm.dpm.ac_power = true;
  1547. else
  1548. adev->pm.dpm.ac_power = false;
  1549. amdgpu_dpm_change_power_state_locked(adev);
  1550. mutex_unlock(&adev->pm.mutex);
  1551. }
  1552. }
  1553. /*
  1554. * Debugfs info
  1555. */
  1556. #if defined(CONFIG_DEBUG_FS)
  1557. static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
  1558. {
  1559. uint32_t value;
  1560. struct pp_gpu_power query = {0};
  1561. int size;
  1562. /* sanity check PP is enabled */
  1563. if (!(adev->powerplay.pp_funcs &&
  1564. adev->powerplay.pp_funcs->read_sensor))
  1565. return -EINVAL;
  1566. /* GPU Clocks */
  1567. size = sizeof(value);
  1568. seq_printf(m, "GFX Clocks and Power:\n");
  1569. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
  1570. seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
  1571. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
  1572. seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
  1573. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
  1574. seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
  1575. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
  1576. seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
  1577. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
  1578. seq_printf(m, "\t%u mV (VDDGFX)\n", value);
  1579. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
  1580. seq_printf(m, "\t%u mV (VDDNB)\n", value);
  1581. size = sizeof(query);
  1582. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) {
  1583. seq_printf(m, "\t%u.%u W (VDDC)\n", query.vddc_power >> 8,
  1584. query.vddc_power & 0xff);
  1585. seq_printf(m, "\t%u.%u W (VDDCI)\n", query.vddci_power >> 8,
  1586. query.vddci_power & 0xff);
  1587. seq_printf(m, "\t%u.%u W (max GPU)\n", query.max_gpu_power >> 8,
  1588. query.max_gpu_power & 0xff);
  1589. seq_printf(m, "\t%u.%u W (average GPU)\n", query.average_gpu_power >> 8,
  1590. query.average_gpu_power & 0xff);
  1591. }
  1592. size = sizeof(value);
  1593. seq_printf(m, "\n");
  1594. /* GPU Temp */
  1595. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
  1596. seq_printf(m, "GPU Temperature: %u C\n", value/1000);
  1597. /* GPU Load */
  1598. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
  1599. seq_printf(m, "GPU Load: %u %%\n", value);
  1600. seq_printf(m, "\n");
  1601. /* UVD clocks */
  1602. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
  1603. if (!value) {
  1604. seq_printf(m, "UVD: Disabled\n");
  1605. } else {
  1606. seq_printf(m, "UVD: Enabled\n");
  1607. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
  1608. seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
  1609. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
  1610. seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
  1611. }
  1612. }
  1613. seq_printf(m, "\n");
  1614. /* VCE clocks */
  1615. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
  1616. if (!value) {
  1617. seq_printf(m, "VCE: Disabled\n");
  1618. } else {
  1619. seq_printf(m, "VCE: Enabled\n");
  1620. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
  1621. seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
  1622. }
  1623. }
  1624. return 0;
  1625. }
  1626. static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
  1627. {
  1628. int i;
  1629. for (i = 0; clocks[i].flag; i++)
  1630. seq_printf(m, "\t%s: %s\n", clocks[i].name,
  1631. (flags & clocks[i].flag) ? "On" : "Off");
  1632. }
  1633. static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
  1634. {
  1635. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1636. struct drm_device *dev = node->minor->dev;
  1637. struct amdgpu_device *adev = dev->dev_private;
  1638. struct drm_device *ddev = adev->ddev;
  1639. u32 flags = 0;
  1640. amdgpu_device_ip_get_clockgating_state(adev, &flags);
  1641. seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
  1642. amdgpu_parse_cg_state(m, flags);
  1643. seq_printf(m, "\n");
  1644. if (!adev->pm.dpm_enabled) {
  1645. seq_printf(m, "dpm not enabled\n");
  1646. return 0;
  1647. }
  1648. if ((adev->flags & AMD_IS_PX) &&
  1649. (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
  1650. seq_printf(m, "PX asic powered off\n");
  1651. } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
  1652. mutex_lock(&adev->pm.mutex);
  1653. if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
  1654. adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
  1655. else
  1656. seq_printf(m, "Debugfs support not implemented for this asic\n");
  1657. mutex_unlock(&adev->pm.mutex);
  1658. } else {
  1659. return amdgpu_debugfs_pm_info_pp(m, adev);
  1660. }
  1661. return 0;
  1662. }
  1663. static const struct drm_info_list amdgpu_pm_info_list[] = {
  1664. {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
  1665. };
  1666. #endif
  1667. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  1668. {
  1669. #if defined(CONFIG_DEBUG_FS)
  1670. return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
  1671. #else
  1672. return 0;
  1673. #endif
  1674. }