amdgpu_pm.c 55 KB

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