amdgpu_pm.c 57 KB

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