amdgpu_pm.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /*
  2. * Permission is hereby granted, free of charge, to any person obtaining a
  3. * copy of this software and associated documentation files (the "Software"),
  4. * to deal in the Software without restriction, including without limitation
  5. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. * and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  16. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  18. * OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * Authors: Rafał Miłecki <zajec5@gmail.com>
  21. * Alex Deucher <alexdeucher@gmail.com>
  22. */
  23. #include <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_drv.h"
  26. #include "amdgpu_pm.h"
  27. #include "amdgpu_dpm.h"
  28. #include "atom.h"
  29. #include <linux/power_supply.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include "amd_powerplay.h"
  33. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
  34. static const struct cg_flag_name clocks[] = {
  35. {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
  36. {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
  37. {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
  38. {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
  39. {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
  40. {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
  41. {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
  42. {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
  43. {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
  44. {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
  45. {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
  46. {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
  47. {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
  48. {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
  49. {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
  50. {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
  51. {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
  52. {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
  53. {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
  54. {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
  55. {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
  56. {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
  57. {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
  58. {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
  59. {0, NULL},
  60. };
  61. void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
  62. {
  63. if (adev->pp_enabled)
  64. /* TODO */
  65. return;
  66. if (adev->pm.dpm_enabled) {
  67. mutex_lock(&adev->pm.mutex);
  68. if (power_supply_is_system_supplied() > 0)
  69. adev->pm.dpm.ac_power = true;
  70. else
  71. adev->pm.dpm.ac_power = false;
  72. if (adev->pm.funcs->enable_bapm)
  73. amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
  74. mutex_unlock(&adev->pm.mutex);
  75. }
  76. }
  77. static ssize_t amdgpu_get_dpm_state(struct device *dev,
  78. struct device_attribute *attr,
  79. char *buf)
  80. {
  81. struct drm_device *ddev = dev_get_drvdata(dev);
  82. struct amdgpu_device *adev = ddev->dev_private;
  83. enum amd_pm_state_type pm;
  84. if (adev->pp_enabled) {
  85. pm = amdgpu_dpm_get_current_power_state(adev);
  86. } else
  87. pm = adev->pm.dpm.user_state;
  88. return snprintf(buf, PAGE_SIZE, "%s\n",
  89. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  90. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  91. }
  92. static ssize_t amdgpu_set_dpm_state(struct device *dev,
  93. struct device_attribute *attr,
  94. const char *buf,
  95. size_t count)
  96. {
  97. struct drm_device *ddev = dev_get_drvdata(dev);
  98. struct amdgpu_device *adev = ddev->dev_private;
  99. enum amd_pm_state_type state;
  100. if (strncmp("battery", buf, strlen("battery")) == 0)
  101. state = POWER_STATE_TYPE_BATTERY;
  102. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  103. state = POWER_STATE_TYPE_BALANCED;
  104. else if (strncmp("performance", buf, strlen("performance")) == 0)
  105. state = POWER_STATE_TYPE_PERFORMANCE;
  106. else {
  107. count = -EINVAL;
  108. goto fail;
  109. }
  110. if (adev->pp_enabled) {
  111. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  112. } else {
  113. mutex_lock(&adev->pm.mutex);
  114. adev->pm.dpm.user_state = state;
  115. mutex_unlock(&adev->pm.mutex);
  116. /* Can't set dpm state when the card is off */
  117. if (!(adev->flags & AMD_IS_PX) ||
  118. (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
  119. amdgpu_pm_compute_clocks(adev);
  120. }
  121. fail:
  122. return count;
  123. }
  124. static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
  125. struct device_attribute *attr,
  126. char *buf)
  127. {
  128. struct drm_device *ddev = dev_get_drvdata(dev);
  129. struct amdgpu_device *adev = ddev->dev_private;
  130. enum amd_dpm_forced_level level;
  131. if ((adev->flags & AMD_IS_PX) &&
  132. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  133. return snprintf(buf, PAGE_SIZE, "off\n");
  134. level = amdgpu_dpm_get_performance_level(adev);
  135. return snprintf(buf, PAGE_SIZE, "%s\n",
  136. (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  137. (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
  138. (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
  139. (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
  140. (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
  141. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
  142. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
  143. (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
  144. "unknown");
  145. }
  146. static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
  147. struct device_attribute *attr,
  148. const char *buf,
  149. size_t count)
  150. {
  151. struct drm_device *ddev = dev_get_drvdata(dev);
  152. struct amdgpu_device *adev = ddev->dev_private;
  153. enum amd_dpm_forced_level level;
  154. enum amd_dpm_forced_level current_level;
  155. int ret = 0;
  156. /* Can't force performance level when the card is off */
  157. if ((adev->flags & AMD_IS_PX) &&
  158. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  159. return -EINVAL;
  160. current_level = amdgpu_dpm_get_performance_level(adev);
  161. if (strncmp("low", buf, strlen("low")) == 0) {
  162. level = AMD_DPM_FORCED_LEVEL_LOW;
  163. } else if (strncmp("high", buf, strlen("high")) == 0) {
  164. level = AMD_DPM_FORCED_LEVEL_HIGH;
  165. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  166. level = AMD_DPM_FORCED_LEVEL_AUTO;
  167. } else if (strncmp("manual", buf, strlen("manual")) == 0) {
  168. level = AMD_DPM_FORCED_LEVEL_MANUAL;
  169. } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
  170. level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
  171. } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
  172. level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
  173. } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
  174. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
  175. } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
  176. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
  177. } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
  178. level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
  179. } else {
  180. count = -EINVAL;
  181. goto fail;
  182. }
  183. if (current_level == level)
  184. return count;
  185. if (adev->pp_enabled)
  186. amdgpu_dpm_force_performance_level(adev, level);
  187. else {
  188. mutex_lock(&adev->pm.mutex);
  189. if (adev->pm.dpm.thermal_active) {
  190. count = -EINVAL;
  191. mutex_unlock(&adev->pm.mutex);
  192. goto fail;
  193. }
  194. ret = amdgpu_dpm_force_performance_level(adev, level);
  195. if (ret)
  196. count = -EINVAL;
  197. else
  198. adev->pm.dpm.forced_level = level;
  199. mutex_unlock(&adev->pm.mutex);
  200. }
  201. fail:
  202. return count;
  203. }
  204. static ssize_t amdgpu_get_pp_num_states(struct device *dev,
  205. struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct drm_device *ddev = dev_get_drvdata(dev);
  209. struct amdgpu_device *adev = ddev->dev_private;
  210. struct pp_states_info data;
  211. int i, buf_len;
  212. if (adev->pp_enabled)
  213. amdgpu_dpm_get_pp_num_states(adev, &data);
  214. buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
  215. for (i = 0; i < data.nums; i++)
  216. buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
  217. (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
  218. (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
  219. (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
  220. (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
  221. return buf_len;
  222. }
  223. static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
  224. struct device_attribute *attr,
  225. char *buf)
  226. {
  227. struct drm_device *ddev = dev_get_drvdata(dev);
  228. struct amdgpu_device *adev = ddev->dev_private;
  229. struct pp_states_info data;
  230. enum amd_pm_state_type pm = 0;
  231. int i = 0;
  232. if (adev->pp_enabled) {
  233. pm = amdgpu_dpm_get_current_power_state(adev);
  234. amdgpu_dpm_get_pp_num_states(adev, &data);
  235. for (i = 0; i < data.nums; i++) {
  236. if (pm == data.states[i])
  237. break;
  238. }
  239. if (i == data.nums)
  240. i = -EINVAL;
  241. }
  242. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  243. }
  244. static ssize_t amdgpu_get_pp_force_state(struct device *dev,
  245. struct device_attribute *attr,
  246. char *buf)
  247. {
  248. struct drm_device *ddev = dev_get_drvdata(dev);
  249. struct amdgpu_device *adev = ddev->dev_private;
  250. struct pp_states_info data;
  251. enum amd_pm_state_type pm = 0;
  252. int i;
  253. if (adev->pp_force_state_enabled && adev->pp_enabled) {
  254. pm = amdgpu_dpm_get_current_power_state(adev);
  255. amdgpu_dpm_get_pp_num_states(adev, &data);
  256. for (i = 0; i < data.nums; i++) {
  257. if (pm == data.states[i])
  258. break;
  259. }
  260. if (i == data.nums)
  261. i = -EINVAL;
  262. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  263. } else
  264. return snprintf(buf, PAGE_SIZE, "\n");
  265. }
  266. static ssize_t amdgpu_set_pp_force_state(struct device *dev,
  267. struct device_attribute *attr,
  268. const char *buf,
  269. size_t count)
  270. {
  271. struct drm_device *ddev = dev_get_drvdata(dev);
  272. struct amdgpu_device *adev = ddev->dev_private;
  273. enum amd_pm_state_type state = 0;
  274. unsigned long idx;
  275. int ret;
  276. if (strlen(buf) == 1)
  277. adev->pp_force_state_enabled = false;
  278. else if (adev->pp_enabled) {
  279. struct pp_states_info data;
  280. ret = kstrtoul(buf, 0, &idx);
  281. if (ret || idx >= ARRAY_SIZE(data.states)) {
  282. count = -EINVAL;
  283. goto fail;
  284. }
  285. amdgpu_dpm_get_pp_num_states(adev, &data);
  286. state = data.states[idx];
  287. /* only set user selected power states */
  288. if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
  289. state != POWER_STATE_TYPE_DEFAULT) {
  290. amdgpu_dpm_dispatch_task(adev,
  291. AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  292. adev->pp_force_state_enabled = true;
  293. }
  294. }
  295. fail:
  296. return count;
  297. }
  298. static ssize_t amdgpu_get_pp_table(struct device *dev,
  299. struct device_attribute *attr,
  300. char *buf)
  301. {
  302. struct drm_device *ddev = dev_get_drvdata(dev);
  303. struct amdgpu_device *adev = ddev->dev_private;
  304. char *table = NULL;
  305. int size;
  306. if (adev->pp_enabled)
  307. size = amdgpu_dpm_get_pp_table(adev, &table);
  308. else
  309. return 0;
  310. if (size >= PAGE_SIZE)
  311. size = PAGE_SIZE - 1;
  312. memcpy(buf, table, size);
  313. return size;
  314. }
  315. static ssize_t amdgpu_set_pp_table(struct device *dev,
  316. struct device_attribute *attr,
  317. const char *buf,
  318. size_t count)
  319. {
  320. struct drm_device *ddev = dev_get_drvdata(dev);
  321. struct amdgpu_device *adev = ddev->dev_private;
  322. if (adev->pp_enabled)
  323. amdgpu_dpm_set_pp_table(adev, buf, count);
  324. return count;
  325. }
  326. static ssize_t amdgpu_get_pp_dpm_sclk(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. ssize_t size = 0;
  333. if (adev->pp_enabled)
  334. size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
  335. else if (adev->pm.funcs->print_clock_levels)
  336. size = adev->pm.funcs->print_clock_levels(adev, PP_SCLK, buf);
  337. return size;
  338. }
  339. static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
  340. struct device_attribute *attr,
  341. const char *buf,
  342. size_t count)
  343. {
  344. struct drm_device *ddev = dev_get_drvdata(dev);
  345. struct amdgpu_device *adev = ddev->dev_private;
  346. int ret;
  347. long level;
  348. uint32_t i, mask = 0;
  349. char sub_str[2];
  350. for (i = 0; i < strlen(buf); i++) {
  351. if (*(buf + i) == '\n')
  352. continue;
  353. sub_str[0] = *(buf + i);
  354. sub_str[1] = '\0';
  355. ret = kstrtol(sub_str, 0, &level);
  356. if (ret) {
  357. count = -EINVAL;
  358. goto fail;
  359. }
  360. mask |= 1 << level;
  361. }
  362. if (adev->pp_enabled)
  363. amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
  364. else if (adev->pm.funcs->force_clock_level)
  365. adev->pm.funcs->force_clock_level(adev, PP_SCLK, mask);
  366. fail:
  367. return count;
  368. }
  369. static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
  370. struct device_attribute *attr,
  371. char *buf)
  372. {
  373. struct drm_device *ddev = dev_get_drvdata(dev);
  374. struct amdgpu_device *adev = ddev->dev_private;
  375. ssize_t size = 0;
  376. if (adev->pp_enabled)
  377. size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
  378. else if (adev->pm.funcs->print_clock_levels)
  379. size = adev->pm.funcs->print_clock_levels(adev, PP_MCLK, buf);
  380. return size;
  381. }
  382. static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
  383. struct device_attribute *attr,
  384. const char *buf,
  385. size_t count)
  386. {
  387. struct drm_device *ddev = dev_get_drvdata(dev);
  388. struct amdgpu_device *adev = ddev->dev_private;
  389. int ret;
  390. long level;
  391. uint32_t i, mask = 0;
  392. char sub_str[2];
  393. for (i = 0; i < strlen(buf); i++) {
  394. if (*(buf + i) == '\n')
  395. continue;
  396. sub_str[0] = *(buf + i);
  397. sub_str[1] = '\0';
  398. ret = kstrtol(sub_str, 0, &level);
  399. if (ret) {
  400. count = -EINVAL;
  401. goto fail;
  402. }
  403. mask |= 1 << level;
  404. }
  405. if (adev->pp_enabled)
  406. amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
  407. else if (adev->pm.funcs->force_clock_level)
  408. adev->pm.funcs->force_clock_level(adev, PP_MCLK, mask);
  409. fail:
  410. return count;
  411. }
  412. static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. struct drm_device *ddev = dev_get_drvdata(dev);
  417. struct amdgpu_device *adev = ddev->dev_private;
  418. ssize_t size = 0;
  419. if (adev->pp_enabled)
  420. size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
  421. else if (adev->pm.funcs->print_clock_levels)
  422. size = adev->pm.funcs->print_clock_levels(adev, PP_PCIE, buf);
  423. return size;
  424. }
  425. static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
  426. struct device_attribute *attr,
  427. const char *buf,
  428. size_t count)
  429. {
  430. struct drm_device *ddev = dev_get_drvdata(dev);
  431. struct amdgpu_device *adev = ddev->dev_private;
  432. int ret;
  433. long level;
  434. uint32_t i, mask = 0;
  435. char sub_str[2];
  436. for (i = 0; i < strlen(buf); i++) {
  437. if (*(buf + i) == '\n')
  438. continue;
  439. sub_str[0] = *(buf + i);
  440. sub_str[1] = '\0';
  441. ret = kstrtol(sub_str, 0, &level);
  442. if (ret) {
  443. count = -EINVAL;
  444. goto fail;
  445. }
  446. mask |= 1 << level;
  447. }
  448. if (adev->pp_enabled)
  449. amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
  450. else if (adev->pm.funcs->force_clock_level)
  451. adev->pm.funcs->force_clock_level(adev, PP_PCIE, mask);
  452. fail:
  453. return count;
  454. }
  455. static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
  456. struct device_attribute *attr,
  457. char *buf)
  458. {
  459. struct drm_device *ddev = dev_get_drvdata(dev);
  460. struct amdgpu_device *adev = ddev->dev_private;
  461. uint32_t value = 0;
  462. if (adev->pp_enabled)
  463. value = amdgpu_dpm_get_sclk_od(adev);
  464. else if (adev->pm.funcs->get_sclk_od)
  465. value = adev->pm.funcs->get_sclk_od(adev);
  466. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  467. }
  468. static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
  469. struct device_attribute *attr,
  470. const char *buf,
  471. size_t count)
  472. {
  473. struct drm_device *ddev = dev_get_drvdata(dev);
  474. struct amdgpu_device *adev = ddev->dev_private;
  475. int ret;
  476. long int value;
  477. ret = kstrtol(buf, 0, &value);
  478. if (ret) {
  479. count = -EINVAL;
  480. goto fail;
  481. }
  482. if (adev->pp_enabled) {
  483. amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
  484. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_READJUST_POWER_STATE, NULL, NULL);
  485. } else if (adev->pm.funcs->set_sclk_od) {
  486. adev->pm.funcs->set_sclk_od(adev, (uint32_t)value);
  487. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  488. amdgpu_pm_compute_clocks(adev);
  489. }
  490. fail:
  491. return count;
  492. }
  493. static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
  494. struct device_attribute *attr,
  495. char *buf)
  496. {
  497. struct drm_device *ddev = dev_get_drvdata(dev);
  498. struct amdgpu_device *adev = ddev->dev_private;
  499. uint32_t value = 0;
  500. if (adev->pp_enabled)
  501. value = amdgpu_dpm_get_mclk_od(adev);
  502. else if (adev->pm.funcs->get_mclk_od)
  503. value = adev->pm.funcs->get_mclk_od(adev);
  504. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  505. }
  506. static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
  507. struct device_attribute *attr,
  508. const char *buf,
  509. size_t count)
  510. {
  511. struct drm_device *ddev = dev_get_drvdata(dev);
  512. struct amdgpu_device *adev = ddev->dev_private;
  513. int ret;
  514. long int value;
  515. ret = kstrtol(buf, 0, &value);
  516. if (ret) {
  517. count = -EINVAL;
  518. goto fail;
  519. }
  520. if (adev->pp_enabled) {
  521. amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
  522. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_READJUST_POWER_STATE, NULL, NULL);
  523. } else if (adev->pm.funcs->set_mclk_od) {
  524. adev->pm.funcs->set_mclk_od(adev, (uint32_t)value);
  525. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  526. amdgpu_pm_compute_clocks(adev);
  527. }
  528. fail:
  529. return count;
  530. }
  531. static ssize_t amdgpu_get_pp_power_profile(struct device *dev,
  532. char *buf, struct amd_pp_profile *query)
  533. {
  534. struct drm_device *ddev = dev_get_drvdata(dev);
  535. struct amdgpu_device *adev = ddev->dev_private;
  536. int ret = 0;
  537. if (adev->pp_enabled)
  538. ret = amdgpu_dpm_get_power_profile_state(
  539. adev, query);
  540. else if (adev->pm.funcs->get_power_profile_state)
  541. ret = adev->pm.funcs->get_power_profile_state(
  542. adev, query);
  543. if (ret)
  544. return ret;
  545. return snprintf(buf, PAGE_SIZE,
  546. "%d %d %d %d %d\n",
  547. query->min_sclk / 100,
  548. query->min_mclk / 100,
  549. query->activity_threshold,
  550. query->up_hyst,
  551. query->down_hyst);
  552. }
  553. static ssize_t amdgpu_get_pp_gfx_power_profile(struct device *dev,
  554. struct device_attribute *attr,
  555. char *buf)
  556. {
  557. struct amd_pp_profile query = {0};
  558. query.type = AMD_PP_GFX_PROFILE;
  559. return amdgpu_get_pp_power_profile(dev, buf, &query);
  560. }
  561. static ssize_t amdgpu_get_pp_compute_power_profile(struct device *dev,
  562. struct device_attribute *attr,
  563. char *buf)
  564. {
  565. struct amd_pp_profile query = {0};
  566. query.type = AMD_PP_COMPUTE_PROFILE;
  567. return amdgpu_get_pp_power_profile(dev, buf, &query);
  568. }
  569. static ssize_t amdgpu_set_pp_power_profile(struct device *dev,
  570. const char *buf,
  571. size_t count,
  572. struct amd_pp_profile *request)
  573. {
  574. struct drm_device *ddev = dev_get_drvdata(dev);
  575. struct amdgpu_device *adev = ddev->dev_private;
  576. uint32_t loop = 0;
  577. char *sub_str, buf_cpy[128], *tmp_str;
  578. const char delimiter[3] = {' ', '\n', '\0'};
  579. long int value;
  580. int ret = 0;
  581. if (strncmp("reset", buf, strlen("reset")) == 0) {
  582. if (adev->pp_enabled)
  583. ret = amdgpu_dpm_reset_power_profile_state(
  584. adev, request);
  585. else if (adev->pm.funcs->reset_power_profile_state)
  586. ret = adev->pm.funcs->reset_power_profile_state(
  587. adev, request);
  588. if (ret) {
  589. count = -EINVAL;
  590. goto fail;
  591. }
  592. return count;
  593. }
  594. if (strncmp("set", buf, strlen("set")) == 0) {
  595. if (adev->pp_enabled)
  596. ret = amdgpu_dpm_set_power_profile_state(
  597. adev, request);
  598. else if (adev->pm.funcs->set_power_profile_state)
  599. ret = adev->pm.funcs->set_power_profile_state(
  600. adev, request);
  601. if (ret) {
  602. count = -EINVAL;
  603. goto fail;
  604. }
  605. return count;
  606. }
  607. if (count + 1 >= 128) {
  608. count = -EINVAL;
  609. goto fail;
  610. }
  611. memcpy(buf_cpy, buf, count + 1);
  612. tmp_str = buf_cpy;
  613. while (tmp_str[0]) {
  614. sub_str = strsep(&tmp_str, delimiter);
  615. ret = kstrtol(sub_str, 0, &value);
  616. if (ret) {
  617. count = -EINVAL;
  618. goto fail;
  619. }
  620. switch (loop) {
  621. case 0:
  622. /* input unit MHz convert to dpm table unit 10KHz*/
  623. request->min_sclk = (uint32_t)value * 100;
  624. break;
  625. case 1:
  626. /* input unit MHz convert to dpm table unit 10KHz*/
  627. request->min_mclk = (uint32_t)value * 100;
  628. break;
  629. case 2:
  630. request->activity_threshold = (uint16_t)value;
  631. break;
  632. case 3:
  633. request->up_hyst = (uint8_t)value;
  634. break;
  635. case 4:
  636. request->down_hyst = (uint8_t)value;
  637. break;
  638. default:
  639. break;
  640. }
  641. loop++;
  642. }
  643. if (adev->pp_enabled)
  644. ret = amdgpu_dpm_set_power_profile_state(
  645. adev, request);
  646. else if (adev->pm.funcs->set_power_profile_state)
  647. ret = adev->pm.funcs->set_power_profile_state(
  648. adev, request);
  649. if (ret)
  650. count = -EINVAL;
  651. fail:
  652. return count;
  653. }
  654. static ssize_t amdgpu_set_pp_gfx_power_profile(struct device *dev,
  655. struct device_attribute *attr,
  656. const char *buf,
  657. size_t count)
  658. {
  659. struct amd_pp_profile request = {0};
  660. request.type = AMD_PP_GFX_PROFILE;
  661. return amdgpu_set_pp_power_profile(dev, buf, count, &request);
  662. }
  663. static ssize_t amdgpu_set_pp_compute_power_profile(struct device *dev,
  664. struct device_attribute *attr,
  665. const char *buf,
  666. size_t count)
  667. {
  668. struct amd_pp_profile request = {0};
  669. request.type = AMD_PP_COMPUTE_PROFILE;
  670. return amdgpu_set_pp_power_profile(dev, buf, count, &request);
  671. }
  672. static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
  673. static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
  674. amdgpu_get_dpm_forced_performance_level,
  675. amdgpu_set_dpm_forced_performance_level);
  676. static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
  677. static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
  678. static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
  679. amdgpu_get_pp_force_state,
  680. amdgpu_set_pp_force_state);
  681. static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
  682. amdgpu_get_pp_table,
  683. amdgpu_set_pp_table);
  684. static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
  685. amdgpu_get_pp_dpm_sclk,
  686. amdgpu_set_pp_dpm_sclk);
  687. static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
  688. amdgpu_get_pp_dpm_mclk,
  689. amdgpu_set_pp_dpm_mclk);
  690. static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
  691. amdgpu_get_pp_dpm_pcie,
  692. amdgpu_set_pp_dpm_pcie);
  693. static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
  694. amdgpu_get_pp_sclk_od,
  695. amdgpu_set_pp_sclk_od);
  696. static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
  697. amdgpu_get_pp_mclk_od,
  698. amdgpu_set_pp_mclk_od);
  699. static DEVICE_ATTR(pp_gfx_power_profile, S_IRUGO | S_IWUSR,
  700. amdgpu_get_pp_gfx_power_profile,
  701. amdgpu_set_pp_gfx_power_profile);
  702. static DEVICE_ATTR(pp_compute_power_profile, S_IRUGO | S_IWUSR,
  703. amdgpu_get_pp_compute_power_profile,
  704. amdgpu_set_pp_compute_power_profile);
  705. static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
  706. struct device_attribute *attr,
  707. char *buf)
  708. {
  709. struct amdgpu_device *adev = dev_get_drvdata(dev);
  710. struct drm_device *ddev = adev->ddev;
  711. int temp;
  712. /* Can't get temperature when the card is off */
  713. if ((adev->flags & AMD_IS_PX) &&
  714. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  715. return -EINVAL;
  716. if (!adev->pp_enabled && !adev->pm.funcs->get_temperature)
  717. temp = 0;
  718. else
  719. temp = amdgpu_dpm_get_temperature(adev);
  720. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  721. }
  722. static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
  723. struct device_attribute *attr,
  724. char *buf)
  725. {
  726. struct amdgpu_device *adev = dev_get_drvdata(dev);
  727. int hyst = to_sensor_dev_attr(attr)->index;
  728. int temp;
  729. if (hyst)
  730. temp = adev->pm.dpm.thermal.min_temp;
  731. else
  732. temp = adev->pm.dpm.thermal.max_temp;
  733. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  734. }
  735. static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
  736. struct device_attribute *attr,
  737. char *buf)
  738. {
  739. struct amdgpu_device *adev = dev_get_drvdata(dev);
  740. u32 pwm_mode = 0;
  741. if (!adev->pp_enabled && !adev->pm.funcs->get_fan_control_mode)
  742. return -EINVAL;
  743. pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
  744. return sprintf(buf, "%i\n", pwm_mode);
  745. }
  746. static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
  747. struct device_attribute *attr,
  748. const char *buf,
  749. size_t count)
  750. {
  751. struct amdgpu_device *adev = dev_get_drvdata(dev);
  752. int err;
  753. int value;
  754. if (!adev->pp_enabled && !adev->pm.funcs->set_fan_control_mode)
  755. return -EINVAL;
  756. err = kstrtoint(buf, 10, &value);
  757. if (err)
  758. return err;
  759. amdgpu_dpm_set_fan_control_mode(adev, value);
  760. return count;
  761. }
  762. static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
  763. struct device_attribute *attr,
  764. char *buf)
  765. {
  766. return sprintf(buf, "%i\n", 0);
  767. }
  768. static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
  769. struct device_attribute *attr,
  770. char *buf)
  771. {
  772. return sprintf(buf, "%i\n", 255);
  773. }
  774. static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
  775. struct device_attribute *attr,
  776. const char *buf, size_t count)
  777. {
  778. struct amdgpu_device *adev = dev_get_drvdata(dev);
  779. int err;
  780. u32 value;
  781. err = kstrtou32(buf, 10, &value);
  782. if (err)
  783. return err;
  784. value = (value * 100) / 255;
  785. err = amdgpu_dpm_set_fan_speed_percent(adev, value);
  786. if (err)
  787. return err;
  788. return count;
  789. }
  790. static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
  791. struct device_attribute *attr,
  792. char *buf)
  793. {
  794. struct amdgpu_device *adev = dev_get_drvdata(dev);
  795. int err;
  796. u32 speed;
  797. err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
  798. if (err)
  799. return err;
  800. speed = (speed * 255) / 100;
  801. return sprintf(buf, "%i\n", speed);
  802. }
  803. static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
  804. struct device_attribute *attr,
  805. char *buf)
  806. {
  807. struct amdgpu_device *adev = dev_get_drvdata(dev);
  808. int err;
  809. u32 speed;
  810. err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
  811. if (err)
  812. return err;
  813. return sprintf(buf, "%i\n", speed);
  814. }
  815. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
  816. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
  817. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
  818. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
  819. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
  820. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
  821. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
  822. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
  823. static struct attribute *hwmon_attributes[] = {
  824. &sensor_dev_attr_temp1_input.dev_attr.attr,
  825. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  826. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  827. &sensor_dev_attr_pwm1.dev_attr.attr,
  828. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  829. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  830. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  831. &sensor_dev_attr_fan1_input.dev_attr.attr,
  832. NULL
  833. };
  834. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  835. struct attribute *attr, int index)
  836. {
  837. struct device *dev = kobj_to_dev(kobj);
  838. struct amdgpu_device *adev = dev_get_drvdata(dev);
  839. umode_t effective_mode = attr->mode;
  840. /* Skip limit attributes if DPM is not enabled */
  841. if (!adev->pm.dpm_enabled &&
  842. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  843. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
  844. attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  845. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  846. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  847. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  848. return 0;
  849. if (adev->pp_enabled)
  850. return effective_mode;
  851. /* Skip fan attributes if fan is not present */
  852. if (adev->pm.no_fan &&
  853. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  854. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  855. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  856. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  857. return 0;
  858. /* mask fan attributes if we have no bindings for this asic to expose */
  859. if ((!adev->pm.funcs->get_fan_speed_percent &&
  860. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  861. (!adev->pm.funcs->get_fan_control_mode &&
  862. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  863. effective_mode &= ~S_IRUGO;
  864. if ((!adev->pm.funcs->set_fan_speed_percent &&
  865. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  866. (!adev->pm.funcs->set_fan_control_mode &&
  867. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  868. effective_mode &= ~S_IWUSR;
  869. /* hide max/min values if we can't both query and manage the fan */
  870. if ((!adev->pm.funcs->set_fan_speed_percent &&
  871. !adev->pm.funcs->get_fan_speed_percent) &&
  872. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  873. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  874. return 0;
  875. /* requires powerplay */
  876. if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
  877. return 0;
  878. return effective_mode;
  879. }
  880. static const struct attribute_group hwmon_attrgroup = {
  881. .attrs = hwmon_attributes,
  882. .is_visible = hwmon_attributes_visible,
  883. };
  884. static const struct attribute_group *hwmon_groups[] = {
  885. &hwmon_attrgroup,
  886. NULL
  887. };
  888. void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
  889. {
  890. struct amdgpu_device *adev =
  891. container_of(work, struct amdgpu_device,
  892. pm.dpm.thermal.work);
  893. /* switch to the thermal state */
  894. enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  895. if (!adev->pm.dpm_enabled)
  896. return;
  897. if (adev->pm.funcs->get_temperature) {
  898. int temp = amdgpu_dpm_get_temperature(adev);
  899. if (temp < adev->pm.dpm.thermal.min_temp)
  900. /* switch back the user state */
  901. dpm_state = adev->pm.dpm.user_state;
  902. } else {
  903. if (adev->pm.dpm.thermal.high_to_low)
  904. /* switch back the user state */
  905. dpm_state = adev->pm.dpm.user_state;
  906. }
  907. mutex_lock(&adev->pm.mutex);
  908. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  909. adev->pm.dpm.thermal_active = true;
  910. else
  911. adev->pm.dpm.thermal_active = false;
  912. adev->pm.dpm.state = dpm_state;
  913. mutex_unlock(&adev->pm.mutex);
  914. amdgpu_pm_compute_clocks(adev);
  915. }
  916. static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
  917. enum amd_pm_state_type dpm_state)
  918. {
  919. int i;
  920. struct amdgpu_ps *ps;
  921. u32 ui_class;
  922. bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
  923. true : false;
  924. /* check if the vblank period is too short to adjust the mclk */
  925. if (single_display && adev->pm.funcs->vblank_too_short) {
  926. if (amdgpu_dpm_vblank_too_short(adev))
  927. single_display = false;
  928. }
  929. /* certain older asics have a separare 3D performance state,
  930. * so try that first if the user selected performance
  931. */
  932. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  933. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  934. /* balanced states don't exist at the moment */
  935. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  936. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  937. restart_search:
  938. /* Pick the best power state based on current conditions */
  939. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  940. ps = &adev->pm.dpm.ps[i];
  941. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  942. switch (dpm_state) {
  943. /* user states */
  944. case POWER_STATE_TYPE_BATTERY:
  945. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  946. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  947. if (single_display)
  948. return ps;
  949. } else
  950. return ps;
  951. }
  952. break;
  953. case POWER_STATE_TYPE_BALANCED:
  954. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  955. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  956. if (single_display)
  957. return ps;
  958. } else
  959. return ps;
  960. }
  961. break;
  962. case POWER_STATE_TYPE_PERFORMANCE:
  963. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  964. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  965. if (single_display)
  966. return ps;
  967. } else
  968. return ps;
  969. }
  970. break;
  971. /* internal states */
  972. case POWER_STATE_TYPE_INTERNAL_UVD:
  973. if (adev->pm.dpm.uvd_ps)
  974. return adev->pm.dpm.uvd_ps;
  975. else
  976. break;
  977. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  978. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  979. return ps;
  980. break;
  981. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  982. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  983. return ps;
  984. break;
  985. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  986. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  987. return ps;
  988. break;
  989. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  990. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  991. return ps;
  992. break;
  993. case POWER_STATE_TYPE_INTERNAL_BOOT:
  994. return adev->pm.dpm.boot_ps;
  995. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  996. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  997. return ps;
  998. break;
  999. case POWER_STATE_TYPE_INTERNAL_ACPI:
  1000. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  1001. return ps;
  1002. break;
  1003. case POWER_STATE_TYPE_INTERNAL_ULV:
  1004. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  1005. return ps;
  1006. break;
  1007. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  1008. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  1009. return ps;
  1010. break;
  1011. default:
  1012. break;
  1013. }
  1014. }
  1015. /* use a fallback state if we didn't match */
  1016. switch (dpm_state) {
  1017. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  1018. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  1019. goto restart_search;
  1020. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  1021. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  1022. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  1023. if (adev->pm.dpm.uvd_ps) {
  1024. return adev->pm.dpm.uvd_ps;
  1025. } else {
  1026. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  1027. goto restart_search;
  1028. }
  1029. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  1030. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  1031. goto restart_search;
  1032. case POWER_STATE_TYPE_INTERNAL_ACPI:
  1033. dpm_state = POWER_STATE_TYPE_BATTERY;
  1034. goto restart_search;
  1035. case POWER_STATE_TYPE_BATTERY:
  1036. case POWER_STATE_TYPE_BALANCED:
  1037. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  1038. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  1039. goto restart_search;
  1040. default:
  1041. break;
  1042. }
  1043. return NULL;
  1044. }
  1045. static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
  1046. {
  1047. struct amdgpu_ps *ps;
  1048. enum amd_pm_state_type dpm_state;
  1049. int ret;
  1050. bool equal;
  1051. /* if dpm init failed */
  1052. if (!adev->pm.dpm_enabled)
  1053. return;
  1054. if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
  1055. /* add other state override checks here */
  1056. if ((!adev->pm.dpm.thermal_active) &&
  1057. (!adev->pm.dpm.uvd_active))
  1058. adev->pm.dpm.state = adev->pm.dpm.user_state;
  1059. }
  1060. dpm_state = adev->pm.dpm.state;
  1061. ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
  1062. if (ps)
  1063. adev->pm.dpm.requested_ps = ps;
  1064. else
  1065. return;
  1066. if (amdgpu_dpm == 1) {
  1067. printk("switching from power state:\n");
  1068. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
  1069. printk("switching to power state:\n");
  1070. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
  1071. }
  1072. /* update whether vce is active */
  1073. ps->vce_active = adev->pm.dpm.vce_active;
  1074. amdgpu_dpm_display_configuration_changed(adev);
  1075. ret = amdgpu_dpm_pre_set_power_state(adev);
  1076. if (ret)
  1077. return;
  1078. if ((0 != amgdpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal)))
  1079. equal = false;
  1080. if (equal)
  1081. return;
  1082. amdgpu_dpm_set_power_state(adev);
  1083. amdgpu_dpm_post_set_power_state(adev);
  1084. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  1085. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  1086. if (adev->pm.funcs->force_performance_level) {
  1087. if (adev->pm.dpm.thermal_active) {
  1088. enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
  1089. /* force low perf level for thermal */
  1090. amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
  1091. /* save the user's level */
  1092. adev->pm.dpm.forced_level = level;
  1093. } else {
  1094. /* otherwise, user selected level */
  1095. amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
  1096. }
  1097. }
  1098. }
  1099. void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
  1100. {
  1101. if (adev->pp_enabled || adev->pm.funcs->powergate_uvd) {
  1102. /* enable/disable UVD */
  1103. mutex_lock(&adev->pm.mutex);
  1104. amdgpu_dpm_powergate_uvd(adev, !enable);
  1105. mutex_unlock(&adev->pm.mutex);
  1106. } else {
  1107. if (enable) {
  1108. mutex_lock(&adev->pm.mutex);
  1109. adev->pm.dpm.uvd_active = true;
  1110. adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
  1111. mutex_unlock(&adev->pm.mutex);
  1112. } else {
  1113. mutex_lock(&adev->pm.mutex);
  1114. adev->pm.dpm.uvd_active = false;
  1115. mutex_unlock(&adev->pm.mutex);
  1116. }
  1117. amdgpu_pm_compute_clocks(adev);
  1118. }
  1119. }
  1120. void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
  1121. {
  1122. if (adev->pp_enabled || adev->pm.funcs->powergate_vce) {
  1123. /* enable/disable VCE */
  1124. mutex_lock(&adev->pm.mutex);
  1125. amdgpu_dpm_powergate_vce(adev, !enable);
  1126. mutex_unlock(&adev->pm.mutex);
  1127. } else {
  1128. if (enable) {
  1129. mutex_lock(&adev->pm.mutex);
  1130. adev->pm.dpm.vce_active = true;
  1131. /* XXX select vce level based on ring/task */
  1132. adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
  1133. mutex_unlock(&adev->pm.mutex);
  1134. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1135. AMD_CG_STATE_UNGATE);
  1136. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1137. AMD_PG_STATE_UNGATE);
  1138. amdgpu_pm_compute_clocks(adev);
  1139. } else {
  1140. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1141. AMD_PG_STATE_GATE);
  1142. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1143. AMD_CG_STATE_GATE);
  1144. mutex_lock(&adev->pm.mutex);
  1145. adev->pm.dpm.vce_active = false;
  1146. mutex_unlock(&adev->pm.mutex);
  1147. amdgpu_pm_compute_clocks(adev);
  1148. }
  1149. }
  1150. }
  1151. void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
  1152. {
  1153. int i;
  1154. if (adev->pp_enabled)
  1155. /* TO DO */
  1156. return;
  1157. for (i = 0; i < adev->pm.dpm.num_ps; i++)
  1158. amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
  1159. }
  1160. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  1161. {
  1162. int ret;
  1163. if (adev->pm.sysfs_initialized)
  1164. return 0;
  1165. if (!adev->pp_enabled) {
  1166. if (adev->pm.funcs->get_temperature == NULL)
  1167. return 0;
  1168. }
  1169. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  1170. DRIVER_NAME, adev,
  1171. hwmon_groups);
  1172. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  1173. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  1174. dev_err(adev->dev,
  1175. "Unable to register hwmon device: %d\n", ret);
  1176. return ret;
  1177. }
  1178. ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
  1179. if (ret) {
  1180. DRM_ERROR("failed to create device file for dpm state\n");
  1181. return ret;
  1182. }
  1183. ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1184. if (ret) {
  1185. DRM_ERROR("failed to create device file for dpm state\n");
  1186. return ret;
  1187. }
  1188. if (adev->pp_enabled) {
  1189. ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
  1190. if (ret) {
  1191. DRM_ERROR("failed to create device file pp_num_states\n");
  1192. return ret;
  1193. }
  1194. ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
  1195. if (ret) {
  1196. DRM_ERROR("failed to create device file pp_cur_state\n");
  1197. return ret;
  1198. }
  1199. ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
  1200. if (ret) {
  1201. DRM_ERROR("failed to create device file pp_force_state\n");
  1202. return ret;
  1203. }
  1204. ret = device_create_file(adev->dev, &dev_attr_pp_table);
  1205. if (ret) {
  1206. DRM_ERROR("failed to create device file pp_table\n");
  1207. return ret;
  1208. }
  1209. }
  1210. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1211. if (ret) {
  1212. DRM_ERROR("failed to create device file pp_dpm_sclk\n");
  1213. return ret;
  1214. }
  1215. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1216. if (ret) {
  1217. DRM_ERROR("failed to create device file pp_dpm_mclk\n");
  1218. return ret;
  1219. }
  1220. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1221. if (ret) {
  1222. DRM_ERROR("failed to create device file pp_dpm_pcie\n");
  1223. return ret;
  1224. }
  1225. ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
  1226. if (ret) {
  1227. DRM_ERROR("failed to create device file pp_sclk_od\n");
  1228. return ret;
  1229. }
  1230. ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
  1231. if (ret) {
  1232. DRM_ERROR("failed to create device file pp_mclk_od\n");
  1233. return ret;
  1234. }
  1235. ret = device_create_file(adev->dev,
  1236. &dev_attr_pp_gfx_power_profile);
  1237. if (ret) {
  1238. DRM_ERROR("failed to create device file "
  1239. "pp_gfx_power_profile\n");
  1240. return ret;
  1241. }
  1242. ret = device_create_file(adev->dev,
  1243. &dev_attr_pp_compute_power_profile);
  1244. if (ret) {
  1245. DRM_ERROR("failed to create device file "
  1246. "pp_compute_power_profile\n");
  1247. return ret;
  1248. }
  1249. ret = amdgpu_debugfs_pm_init(adev);
  1250. if (ret) {
  1251. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  1252. return ret;
  1253. }
  1254. adev->pm.sysfs_initialized = true;
  1255. return 0;
  1256. }
  1257. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  1258. {
  1259. if (adev->pm.int_hwmon_dev)
  1260. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  1261. device_remove_file(adev->dev, &dev_attr_power_dpm_state);
  1262. device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1263. if (adev->pp_enabled) {
  1264. device_remove_file(adev->dev, &dev_attr_pp_num_states);
  1265. device_remove_file(adev->dev, &dev_attr_pp_cur_state);
  1266. device_remove_file(adev->dev, &dev_attr_pp_force_state);
  1267. device_remove_file(adev->dev, &dev_attr_pp_table);
  1268. }
  1269. device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1270. device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1271. device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1272. device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
  1273. device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
  1274. device_remove_file(adev->dev,
  1275. &dev_attr_pp_gfx_power_profile);
  1276. device_remove_file(adev->dev,
  1277. &dev_attr_pp_compute_power_profile);
  1278. }
  1279. void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
  1280. {
  1281. struct drm_device *ddev = adev->ddev;
  1282. struct drm_crtc *crtc;
  1283. struct amdgpu_crtc *amdgpu_crtc;
  1284. int i = 0;
  1285. if (!adev->pm.dpm_enabled)
  1286. return;
  1287. if (adev->mode_info.num_crtc)
  1288. amdgpu_display_bandwidth_update(adev);
  1289. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  1290. struct amdgpu_ring *ring = adev->rings[i];
  1291. if (ring && ring->ready)
  1292. amdgpu_fence_wait_empty(ring);
  1293. }
  1294. if (adev->pp_enabled) {
  1295. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE, NULL, NULL);
  1296. } else {
  1297. mutex_lock(&adev->pm.mutex);
  1298. adev->pm.dpm.new_active_crtcs = 0;
  1299. adev->pm.dpm.new_active_crtc_count = 0;
  1300. if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
  1301. list_for_each_entry(crtc,
  1302. &ddev->mode_config.crtc_list, head) {
  1303. amdgpu_crtc = to_amdgpu_crtc(crtc);
  1304. if (crtc->enabled) {
  1305. adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
  1306. adev->pm.dpm.new_active_crtc_count++;
  1307. }
  1308. }
  1309. }
  1310. /* update battery/ac status */
  1311. if (power_supply_is_system_supplied() > 0)
  1312. adev->pm.dpm.ac_power = true;
  1313. else
  1314. adev->pm.dpm.ac_power = false;
  1315. amdgpu_dpm_change_power_state_locked(adev);
  1316. mutex_unlock(&adev->pm.mutex);
  1317. }
  1318. }
  1319. /*
  1320. * Debugfs info
  1321. */
  1322. #if defined(CONFIG_DEBUG_FS)
  1323. static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
  1324. {
  1325. uint32_t value;
  1326. struct pp_gpu_power query = {0};
  1327. int size;
  1328. /* sanity check PP is enabled */
  1329. if (!(adev->powerplay.pp_funcs &&
  1330. adev->powerplay.pp_funcs->read_sensor))
  1331. return -EINVAL;
  1332. /* GPU Clocks */
  1333. size = sizeof(value);
  1334. seq_printf(m, "GFX Clocks and Power:\n");
  1335. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
  1336. seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
  1337. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
  1338. seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
  1339. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
  1340. seq_printf(m, "\t%u mV (VDDGFX)\n", value);
  1341. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
  1342. seq_printf(m, "\t%u mV (VDDNB)\n", value);
  1343. size = sizeof(query);
  1344. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) {
  1345. seq_printf(m, "\t%u.%u W (VDDC)\n", query.vddc_power >> 8,
  1346. query.vddc_power & 0xff);
  1347. seq_printf(m, "\t%u.%u W (VDDCI)\n", query.vddci_power >> 8,
  1348. query.vddci_power & 0xff);
  1349. seq_printf(m, "\t%u.%u W (max GPU)\n", query.max_gpu_power >> 8,
  1350. query.max_gpu_power & 0xff);
  1351. seq_printf(m, "\t%u.%u W (average GPU)\n", query.average_gpu_power >> 8,
  1352. query.average_gpu_power & 0xff);
  1353. }
  1354. size = sizeof(value);
  1355. seq_printf(m, "\n");
  1356. /* GPU Temp */
  1357. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
  1358. seq_printf(m, "GPU Temperature: %u C\n", value/1000);
  1359. /* GPU Load */
  1360. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
  1361. seq_printf(m, "GPU Load: %u %%\n", value);
  1362. seq_printf(m, "\n");
  1363. /* UVD clocks */
  1364. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
  1365. if (!value) {
  1366. seq_printf(m, "UVD: Disabled\n");
  1367. } else {
  1368. seq_printf(m, "UVD: Enabled\n");
  1369. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
  1370. seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
  1371. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
  1372. seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
  1373. }
  1374. }
  1375. seq_printf(m, "\n");
  1376. /* VCE clocks */
  1377. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
  1378. if (!value) {
  1379. seq_printf(m, "VCE: Disabled\n");
  1380. } else {
  1381. seq_printf(m, "VCE: Enabled\n");
  1382. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
  1383. seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
  1384. }
  1385. }
  1386. return 0;
  1387. }
  1388. static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
  1389. {
  1390. int i;
  1391. for (i = 0; clocks[i].flag; i++)
  1392. seq_printf(m, "\t%s: %s\n", clocks[i].name,
  1393. (flags & clocks[i].flag) ? "On" : "Off");
  1394. }
  1395. static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
  1396. {
  1397. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1398. struct drm_device *dev = node->minor->dev;
  1399. struct amdgpu_device *adev = dev->dev_private;
  1400. struct drm_device *ddev = adev->ddev;
  1401. u32 flags = 0;
  1402. amdgpu_get_clockgating_state(adev, &flags);
  1403. seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
  1404. amdgpu_parse_cg_state(m, flags);
  1405. seq_printf(m, "\n");
  1406. if (!adev->pm.dpm_enabled) {
  1407. seq_printf(m, "dpm not enabled\n");
  1408. return 0;
  1409. }
  1410. if ((adev->flags & AMD_IS_PX) &&
  1411. (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
  1412. seq_printf(m, "PX asic powered off\n");
  1413. } else if (adev->pp_enabled) {
  1414. return amdgpu_debugfs_pm_info_pp(m, adev);
  1415. } else {
  1416. mutex_lock(&adev->pm.mutex);
  1417. if (adev->pm.funcs->debugfs_print_current_performance_level)
  1418. adev->pm.funcs->debugfs_print_current_performance_level(adev, m);
  1419. else
  1420. seq_printf(m, "Debugfs support not implemented for this asic\n");
  1421. mutex_unlock(&adev->pm.mutex);
  1422. }
  1423. return 0;
  1424. }
  1425. static const struct drm_info_list amdgpu_pm_info_list[] = {
  1426. {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
  1427. };
  1428. #endif
  1429. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  1430. {
  1431. #if defined(CONFIG_DEBUG_FS)
  1432. return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
  1433. #else
  1434. return 0;
  1435. #endif
  1436. }