amdgpu_pm.c 56 KB

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