amdgpu_pm.c 60 KB

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