amdgpu_pm.c 67 KB

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