amdgpu_pm.c 60 KB

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