amdgpu_pm.c 48 KB

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