amdgpu_pm.c 61 KB

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