amdgpu_pm.c 46 KB

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