amdgpu_pm.c 61 KB

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