amdgpu_pm.c 60 KB

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