amdgpu_pm.c 58 KB

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