amdgpu_pm.c 67 KB

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