amdgpu_pm.c 60 KB

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