amdgpu_pm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
  35. {
  36. if (adev->pp_enabled)
  37. /* TODO */
  38. return;
  39. if (adev->pm.dpm_enabled) {
  40. mutex_lock(&adev->pm.mutex);
  41. if (power_supply_is_system_supplied() > 0)
  42. adev->pm.dpm.ac_power = true;
  43. else
  44. adev->pm.dpm.ac_power = false;
  45. if (adev->pm.funcs->enable_bapm)
  46. amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
  47. mutex_unlock(&adev->pm.mutex);
  48. }
  49. }
  50. static ssize_t amdgpu_get_dpm_state(struct device *dev,
  51. struct device_attribute *attr,
  52. char *buf)
  53. {
  54. struct drm_device *ddev = dev_get_drvdata(dev);
  55. struct amdgpu_device *adev = ddev->dev_private;
  56. enum amd_pm_state_type pm;
  57. if (adev->pp_enabled) {
  58. pm = amdgpu_dpm_get_current_power_state(adev);
  59. } else
  60. pm = adev->pm.dpm.user_state;
  61. return snprintf(buf, PAGE_SIZE, "%s\n",
  62. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  63. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  64. }
  65. static ssize_t amdgpu_set_dpm_state(struct device *dev,
  66. struct device_attribute *attr,
  67. const char *buf,
  68. size_t count)
  69. {
  70. struct drm_device *ddev = dev_get_drvdata(dev);
  71. struct amdgpu_device *adev = ddev->dev_private;
  72. enum amd_pm_state_type state;
  73. if (strncmp("battery", buf, strlen("battery")) == 0)
  74. state = POWER_STATE_TYPE_BATTERY;
  75. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  76. state = POWER_STATE_TYPE_BALANCED;
  77. else if (strncmp("performance", buf, strlen("performance")) == 0)
  78. state = POWER_STATE_TYPE_PERFORMANCE;
  79. else {
  80. count = -EINVAL;
  81. goto fail;
  82. }
  83. if (adev->pp_enabled) {
  84. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  85. } else {
  86. mutex_lock(&adev->pm.mutex);
  87. adev->pm.dpm.user_state = state;
  88. mutex_unlock(&adev->pm.mutex);
  89. /* Can't set dpm state when the card is off */
  90. if (!(adev->flags & AMD_IS_PX) ||
  91. (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
  92. amdgpu_pm_compute_clocks(adev);
  93. }
  94. fail:
  95. return count;
  96. }
  97. static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
  98. struct device_attribute *attr,
  99. char *buf)
  100. {
  101. struct drm_device *ddev = dev_get_drvdata(dev);
  102. struct amdgpu_device *adev = ddev->dev_private;
  103. if ((adev->flags & AMD_IS_PX) &&
  104. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  105. return snprintf(buf, PAGE_SIZE, "off\n");
  106. if (adev->pp_enabled) {
  107. enum amd_dpm_forced_level level;
  108. level = amdgpu_dpm_get_performance_level(adev);
  109. return snprintf(buf, PAGE_SIZE, "%s\n",
  110. (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  111. (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
  112. (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
  113. (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" : "unknown");
  114. } else {
  115. enum amdgpu_dpm_forced_level level;
  116. level = adev->pm.dpm.forced_level;
  117. return snprintf(buf, PAGE_SIZE, "%s\n",
  118. (level == AMDGPU_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  119. (level == AMDGPU_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
  120. }
  121. }
  122. static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
  123. struct device_attribute *attr,
  124. const char *buf,
  125. size_t count)
  126. {
  127. struct drm_device *ddev = dev_get_drvdata(dev);
  128. struct amdgpu_device *adev = ddev->dev_private;
  129. enum amdgpu_dpm_forced_level level;
  130. int ret = 0;
  131. /* Can't force performance level when the card is off */
  132. if ((adev->flags & AMD_IS_PX) &&
  133. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  134. return -EINVAL;
  135. if (strncmp("low", buf, strlen("low")) == 0) {
  136. level = AMDGPU_DPM_FORCED_LEVEL_LOW;
  137. } else if (strncmp("high", buf, strlen("high")) == 0) {
  138. level = AMDGPU_DPM_FORCED_LEVEL_HIGH;
  139. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  140. level = AMDGPU_DPM_FORCED_LEVEL_AUTO;
  141. } else if (strncmp("manual", buf, strlen("manual")) == 0) {
  142. level = AMDGPU_DPM_FORCED_LEVEL_MANUAL;
  143. } else {
  144. count = -EINVAL;
  145. goto fail;
  146. }
  147. if (adev->pp_enabled)
  148. amdgpu_dpm_force_performance_level(adev, level);
  149. else {
  150. mutex_lock(&adev->pm.mutex);
  151. if (adev->pm.dpm.thermal_active) {
  152. count = -EINVAL;
  153. mutex_unlock(&adev->pm.mutex);
  154. goto fail;
  155. }
  156. ret = amdgpu_dpm_force_performance_level(adev, level);
  157. if (ret)
  158. count = -EINVAL;
  159. else
  160. adev->pm.dpm.forced_level = level;
  161. mutex_unlock(&adev->pm.mutex);
  162. }
  163. fail:
  164. return count;
  165. }
  166. static ssize_t amdgpu_get_pp_num_states(struct device *dev,
  167. struct device_attribute *attr,
  168. char *buf)
  169. {
  170. struct drm_device *ddev = dev_get_drvdata(dev);
  171. struct amdgpu_device *adev = ddev->dev_private;
  172. struct pp_states_info data;
  173. int i, buf_len;
  174. if (adev->pp_enabled)
  175. amdgpu_dpm_get_pp_num_states(adev, &data);
  176. buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
  177. for (i = 0; i < data.nums; i++)
  178. buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
  179. (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
  180. (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
  181. (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
  182. (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
  183. return buf_len;
  184. }
  185. static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
  186. struct device_attribute *attr,
  187. char *buf)
  188. {
  189. struct drm_device *ddev = dev_get_drvdata(dev);
  190. struct amdgpu_device *adev = ddev->dev_private;
  191. struct pp_states_info data;
  192. enum amd_pm_state_type pm = 0;
  193. int i = 0;
  194. if (adev->pp_enabled) {
  195. pm = amdgpu_dpm_get_current_power_state(adev);
  196. amdgpu_dpm_get_pp_num_states(adev, &data);
  197. for (i = 0; i < data.nums; i++) {
  198. if (pm == data.states[i])
  199. break;
  200. }
  201. if (i == data.nums)
  202. i = -EINVAL;
  203. }
  204. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  205. }
  206. static ssize_t amdgpu_get_pp_force_state(struct device *dev,
  207. struct device_attribute *attr,
  208. char *buf)
  209. {
  210. struct drm_device *ddev = dev_get_drvdata(dev);
  211. struct amdgpu_device *adev = ddev->dev_private;
  212. struct pp_states_info data;
  213. enum amd_pm_state_type pm = 0;
  214. int i;
  215. if (adev->pp_force_state_enabled && adev->pp_enabled) {
  216. pm = amdgpu_dpm_get_current_power_state(adev);
  217. amdgpu_dpm_get_pp_num_states(adev, &data);
  218. for (i = 0; i < data.nums; i++) {
  219. if (pm == data.states[i])
  220. break;
  221. }
  222. if (i == data.nums)
  223. i = -EINVAL;
  224. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  225. } else
  226. return snprintf(buf, PAGE_SIZE, "\n");
  227. }
  228. static ssize_t amdgpu_set_pp_force_state(struct device *dev,
  229. struct device_attribute *attr,
  230. const char *buf,
  231. size_t count)
  232. {
  233. struct drm_device *ddev = dev_get_drvdata(dev);
  234. struct amdgpu_device *adev = ddev->dev_private;
  235. enum amd_pm_state_type state = 0;
  236. unsigned long idx;
  237. int ret;
  238. if (strlen(buf) == 1)
  239. adev->pp_force_state_enabled = false;
  240. else if (adev->pp_enabled) {
  241. struct pp_states_info data;
  242. ret = kstrtoul(buf, 0, &idx);
  243. if (ret || idx >= ARRAY_SIZE(data.states)) {
  244. count = -EINVAL;
  245. goto fail;
  246. }
  247. amdgpu_dpm_get_pp_num_states(adev, &data);
  248. state = data.states[idx];
  249. /* only set user selected power states */
  250. if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
  251. state != POWER_STATE_TYPE_DEFAULT) {
  252. amdgpu_dpm_dispatch_task(adev,
  253. AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  254. adev->pp_force_state_enabled = true;
  255. }
  256. }
  257. fail:
  258. return count;
  259. }
  260. static ssize_t amdgpu_get_pp_table(struct device *dev,
  261. struct device_attribute *attr,
  262. char *buf)
  263. {
  264. struct drm_device *ddev = dev_get_drvdata(dev);
  265. struct amdgpu_device *adev = ddev->dev_private;
  266. char *table = NULL;
  267. int size;
  268. if (adev->pp_enabled)
  269. size = amdgpu_dpm_get_pp_table(adev, &table);
  270. else
  271. return 0;
  272. if (size >= PAGE_SIZE)
  273. size = PAGE_SIZE - 1;
  274. memcpy(buf, table, size);
  275. return size;
  276. }
  277. static ssize_t amdgpu_set_pp_table(struct device *dev,
  278. struct device_attribute *attr,
  279. const char *buf,
  280. size_t count)
  281. {
  282. struct drm_device *ddev = dev_get_drvdata(dev);
  283. struct amdgpu_device *adev = ddev->dev_private;
  284. if (adev->pp_enabled)
  285. amdgpu_dpm_set_pp_table(adev, buf, count);
  286. return count;
  287. }
  288. static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
  289. struct device_attribute *attr,
  290. char *buf)
  291. {
  292. struct drm_device *ddev = dev_get_drvdata(dev);
  293. struct amdgpu_device *adev = ddev->dev_private;
  294. ssize_t size = 0;
  295. if (adev->pp_enabled)
  296. size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
  297. else if (adev->pm.funcs->print_clock_levels)
  298. size = adev->pm.funcs->print_clock_levels(adev, PP_SCLK, buf);
  299. return size;
  300. }
  301. static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
  302. struct device_attribute *attr,
  303. const char *buf,
  304. size_t count)
  305. {
  306. struct drm_device *ddev = dev_get_drvdata(dev);
  307. struct amdgpu_device *adev = ddev->dev_private;
  308. int ret;
  309. long level;
  310. uint32_t i, mask = 0;
  311. char sub_str[2];
  312. for (i = 0; i < strlen(buf); i++) {
  313. if (*(buf + i) == '\n')
  314. continue;
  315. sub_str[0] = *(buf + i);
  316. sub_str[1] = '\0';
  317. ret = kstrtol(sub_str, 0, &level);
  318. if (ret) {
  319. count = -EINVAL;
  320. goto fail;
  321. }
  322. mask |= 1 << level;
  323. }
  324. if (adev->pp_enabled)
  325. amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
  326. else if (adev->pm.funcs->force_clock_level)
  327. adev->pm.funcs->force_clock_level(adev, PP_SCLK, mask);
  328. fail:
  329. return count;
  330. }
  331. static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
  332. struct device_attribute *attr,
  333. char *buf)
  334. {
  335. struct drm_device *ddev = dev_get_drvdata(dev);
  336. struct amdgpu_device *adev = ddev->dev_private;
  337. ssize_t size = 0;
  338. if (adev->pp_enabled)
  339. size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
  340. else if (adev->pm.funcs->print_clock_levels)
  341. size = adev->pm.funcs->print_clock_levels(adev, PP_MCLK, buf);
  342. return size;
  343. }
  344. static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
  345. struct device_attribute *attr,
  346. const char *buf,
  347. size_t count)
  348. {
  349. struct drm_device *ddev = dev_get_drvdata(dev);
  350. struct amdgpu_device *adev = ddev->dev_private;
  351. int ret;
  352. long level;
  353. uint32_t i, mask = 0;
  354. char sub_str[2];
  355. for (i = 0; i < strlen(buf); i++) {
  356. if (*(buf + i) == '\n')
  357. continue;
  358. sub_str[0] = *(buf + i);
  359. sub_str[1] = '\0';
  360. ret = kstrtol(sub_str, 0, &level);
  361. if (ret) {
  362. count = -EINVAL;
  363. goto fail;
  364. }
  365. mask |= 1 << level;
  366. }
  367. if (adev->pp_enabled)
  368. amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
  369. else if (adev->pm.funcs->force_clock_level)
  370. adev->pm.funcs->force_clock_level(adev, PP_MCLK, mask);
  371. fail:
  372. return count;
  373. }
  374. static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
  375. struct device_attribute *attr,
  376. char *buf)
  377. {
  378. struct drm_device *ddev = dev_get_drvdata(dev);
  379. struct amdgpu_device *adev = ddev->dev_private;
  380. ssize_t size = 0;
  381. if (adev->pp_enabled)
  382. size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
  383. else if (adev->pm.funcs->print_clock_levels)
  384. size = adev->pm.funcs->print_clock_levels(adev, PP_PCIE, buf);
  385. return size;
  386. }
  387. static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
  388. struct device_attribute *attr,
  389. const char *buf,
  390. size_t count)
  391. {
  392. struct drm_device *ddev = dev_get_drvdata(dev);
  393. struct amdgpu_device *adev = ddev->dev_private;
  394. int ret;
  395. long level;
  396. uint32_t i, mask = 0;
  397. char sub_str[2];
  398. for (i = 0; i < strlen(buf); i++) {
  399. if (*(buf + i) == '\n')
  400. continue;
  401. sub_str[0] = *(buf + i);
  402. sub_str[1] = '\0';
  403. ret = kstrtol(sub_str, 0, &level);
  404. if (ret) {
  405. count = -EINVAL;
  406. goto fail;
  407. }
  408. mask |= 1 << level;
  409. }
  410. if (adev->pp_enabled)
  411. amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
  412. else if (adev->pm.funcs->force_clock_level)
  413. adev->pm.funcs->force_clock_level(adev, PP_PCIE, mask);
  414. fail:
  415. return count;
  416. }
  417. static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
  418. struct device_attribute *attr,
  419. char *buf)
  420. {
  421. struct drm_device *ddev = dev_get_drvdata(dev);
  422. struct amdgpu_device *adev = ddev->dev_private;
  423. uint32_t value = 0;
  424. if (adev->pp_enabled)
  425. value = amdgpu_dpm_get_sclk_od(adev);
  426. else if (adev->pm.funcs->get_sclk_od)
  427. value = adev->pm.funcs->get_sclk_od(adev);
  428. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  429. }
  430. static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
  431. struct device_attribute *attr,
  432. const char *buf,
  433. size_t count)
  434. {
  435. struct drm_device *ddev = dev_get_drvdata(dev);
  436. struct amdgpu_device *adev = ddev->dev_private;
  437. int ret;
  438. long int value;
  439. ret = kstrtol(buf, 0, &value);
  440. if (ret) {
  441. count = -EINVAL;
  442. goto fail;
  443. }
  444. if (adev->pp_enabled) {
  445. amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
  446. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_READJUST_POWER_STATE, NULL, NULL);
  447. } else if (adev->pm.funcs->set_sclk_od) {
  448. adev->pm.funcs->set_sclk_od(adev, (uint32_t)value);
  449. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  450. amdgpu_pm_compute_clocks(adev);
  451. }
  452. fail:
  453. return count;
  454. }
  455. static ssize_t amdgpu_get_pp_mclk_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_mclk_od(adev);
  464. else if (adev->pm.funcs->get_mclk_od)
  465. value = adev->pm.funcs->get_mclk_od(adev);
  466. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  467. }
  468. static ssize_t amdgpu_set_pp_mclk_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_mclk_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_mclk_od) {
  486. adev->pm.funcs->set_mclk_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 DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
  494. static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
  495. amdgpu_get_dpm_forced_performance_level,
  496. amdgpu_set_dpm_forced_performance_level);
  497. static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
  498. static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
  499. static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
  500. amdgpu_get_pp_force_state,
  501. amdgpu_set_pp_force_state);
  502. static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
  503. amdgpu_get_pp_table,
  504. amdgpu_set_pp_table);
  505. static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
  506. amdgpu_get_pp_dpm_sclk,
  507. amdgpu_set_pp_dpm_sclk);
  508. static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
  509. amdgpu_get_pp_dpm_mclk,
  510. amdgpu_set_pp_dpm_mclk);
  511. static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
  512. amdgpu_get_pp_dpm_pcie,
  513. amdgpu_set_pp_dpm_pcie);
  514. static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
  515. amdgpu_get_pp_sclk_od,
  516. amdgpu_set_pp_sclk_od);
  517. static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
  518. amdgpu_get_pp_mclk_od,
  519. amdgpu_set_pp_mclk_od);
  520. static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
  521. struct device_attribute *attr,
  522. char *buf)
  523. {
  524. struct amdgpu_device *adev = dev_get_drvdata(dev);
  525. struct drm_device *ddev = adev->ddev;
  526. int temp;
  527. /* Can't get temperature when the card is off */
  528. if ((adev->flags & AMD_IS_PX) &&
  529. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  530. return -EINVAL;
  531. if (!adev->pp_enabled && !adev->pm.funcs->get_temperature)
  532. temp = 0;
  533. else
  534. temp = amdgpu_dpm_get_temperature(adev);
  535. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  536. }
  537. static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
  538. struct device_attribute *attr,
  539. char *buf)
  540. {
  541. struct amdgpu_device *adev = dev_get_drvdata(dev);
  542. int hyst = to_sensor_dev_attr(attr)->index;
  543. int temp;
  544. if (hyst)
  545. temp = adev->pm.dpm.thermal.min_temp;
  546. else
  547. temp = adev->pm.dpm.thermal.max_temp;
  548. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  549. }
  550. static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
  551. struct device_attribute *attr,
  552. char *buf)
  553. {
  554. struct amdgpu_device *adev = dev_get_drvdata(dev);
  555. u32 pwm_mode = 0;
  556. if (!adev->pp_enabled && !adev->pm.funcs->get_fan_control_mode)
  557. return -EINVAL;
  558. pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
  559. /* never 0 (full-speed), fuse or smc-controlled always */
  560. return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
  561. }
  562. static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
  563. struct device_attribute *attr,
  564. const char *buf,
  565. size_t count)
  566. {
  567. struct amdgpu_device *adev = dev_get_drvdata(dev);
  568. int err;
  569. int value;
  570. if (!adev->pp_enabled && !adev->pm.funcs->set_fan_control_mode)
  571. return -EINVAL;
  572. err = kstrtoint(buf, 10, &value);
  573. if (err)
  574. return err;
  575. switch (value) {
  576. case 1: /* manual, percent-based */
  577. amdgpu_dpm_set_fan_control_mode(adev, FDO_PWM_MODE_STATIC);
  578. break;
  579. default: /* disable */
  580. amdgpu_dpm_set_fan_control_mode(adev, 0);
  581. break;
  582. }
  583. return count;
  584. }
  585. static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
  586. struct device_attribute *attr,
  587. char *buf)
  588. {
  589. return sprintf(buf, "%i\n", 0);
  590. }
  591. static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
  592. struct device_attribute *attr,
  593. char *buf)
  594. {
  595. return sprintf(buf, "%i\n", 255);
  596. }
  597. static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
  598. struct device_attribute *attr,
  599. const char *buf, size_t count)
  600. {
  601. struct amdgpu_device *adev = dev_get_drvdata(dev);
  602. int err;
  603. u32 value;
  604. err = kstrtou32(buf, 10, &value);
  605. if (err)
  606. return err;
  607. value = (value * 100) / 255;
  608. err = amdgpu_dpm_set_fan_speed_percent(adev, value);
  609. if (err)
  610. return err;
  611. return count;
  612. }
  613. static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
  614. struct device_attribute *attr,
  615. char *buf)
  616. {
  617. struct amdgpu_device *adev = dev_get_drvdata(dev);
  618. int err;
  619. u32 speed;
  620. err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
  621. if (err)
  622. return err;
  623. speed = (speed * 255) / 100;
  624. return sprintf(buf, "%i\n", speed);
  625. }
  626. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
  627. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
  628. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
  629. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
  630. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
  631. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
  632. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
  633. static struct attribute *hwmon_attributes[] = {
  634. &sensor_dev_attr_temp1_input.dev_attr.attr,
  635. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  636. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  637. &sensor_dev_attr_pwm1.dev_attr.attr,
  638. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  639. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  640. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  641. NULL
  642. };
  643. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  644. struct attribute *attr, int index)
  645. {
  646. struct device *dev = kobj_to_dev(kobj);
  647. struct amdgpu_device *adev = dev_get_drvdata(dev);
  648. umode_t effective_mode = attr->mode;
  649. /* Skip limit attributes if DPM is not enabled */
  650. if (!adev->pm.dpm_enabled &&
  651. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  652. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
  653. attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  654. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  655. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  656. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  657. return 0;
  658. if (adev->pp_enabled)
  659. return effective_mode;
  660. /* Skip fan attributes if fan is not present */
  661. if (adev->pm.no_fan &&
  662. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  663. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  664. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  665. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  666. return 0;
  667. /* mask fan attributes if we have no bindings for this asic to expose */
  668. if ((!adev->pm.funcs->get_fan_speed_percent &&
  669. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  670. (!adev->pm.funcs->get_fan_control_mode &&
  671. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  672. effective_mode &= ~S_IRUGO;
  673. if ((!adev->pm.funcs->set_fan_speed_percent &&
  674. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  675. (!adev->pm.funcs->set_fan_control_mode &&
  676. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  677. effective_mode &= ~S_IWUSR;
  678. /* hide max/min values if we can't both query and manage the fan */
  679. if ((!adev->pm.funcs->set_fan_speed_percent &&
  680. !adev->pm.funcs->get_fan_speed_percent) &&
  681. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  682. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  683. return 0;
  684. return effective_mode;
  685. }
  686. static const struct attribute_group hwmon_attrgroup = {
  687. .attrs = hwmon_attributes,
  688. .is_visible = hwmon_attributes_visible,
  689. };
  690. static const struct attribute_group *hwmon_groups[] = {
  691. &hwmon_attrgroup,
  692. NULL
  693. };
  694. void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
  695. {
  696. struct amdgpu_device *adev =
  697. container_of(work, struct amdgpu_device,
  698. pm.dpm.thermal.work);
  699. /* switch to the thermal state */
  700. enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  701. if (!adev->pm.dpm_enabled)
  702. return;
  703. if (adev->pm.funcs->get_temperature) {
  704. int temp = amdgpu_dpm_get_temperature(adev);
  705. if (temp < adev->pm.dpm.thermal.min_temp)
  706. /* switch back the user state */
  707. dpm_state = adev->pm.dpm.user_state;
  708. } else {
  709. if (adev->pm.dpm.thermal.high_to_low)
  710. /* switch back the user state */
  711. dpm_state = adev->pm.dpm.user_state;
  712. }
  713. mutex_lock(&adev->pm.mutex);
  714. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  715. adev->pm.dpm.thermal_active = true;
  716. else
  717. adev->pm.dpm.thermal_active = false;
  718. adev->pm.dpm.state = dpm_state;
  719. mutex_unlock(&adev->pm.mutex);
  720. amdgpu_pm_compute_clocks(adev);
  721. }
  722. static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
  723. enum amd_pm_state_type dpm_state)
  724. {
  725. int i;
  726. struct amdgpu_ps *ps;
  727. u32 ui_class;
  728. bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
  729. true : false;
  730. /* check if the vblank period is too short to adjust the mclk */
  731. if (single_display && adev->pm.funcs->vblank_too_short) {
  732. if (amdgpu_dpm_vblank_too_short(adev))
  733. single_display = false;
  734. }
  735. /* certain older asics have a separare 3D performance state,
  736. * so try that first if the user selected performance
  737. */
  738. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  739. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  740. /* balanced states don't exist at the moment */
  741. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  742. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  743. restart_search:
  744. /* Pick the best power state based on current conditions */
  745. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  746. ps = &adev->pm.dpm.ps[i];
  747. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  748. switch (dpm_state) {
  749. /* user states */
  750. case POWER_STATE_TYPE_BATTERY:
  751. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  752. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  753. if (single_display)
  754. return ps;
  755. } else
  756. return ps;
  757. }
  758. break;
  759. case POWER_STATE_TYPE_BALANCED:
  760. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  761. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  762. if (single_display)
  763. return ps;
  764. } else
  765. return ps;
  766. }
  767. break;
  768. case POWER_STATE_TYPE_PERFORMANCE:
  769. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  770. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  771. if (single_display)
  772. return ps;
  773. } else
  774. return ps;
  775. }
  776. break;
  777. /* internal states */
  778. case POWER_STATE_TYPE_INTERNAL_UVD:
  779. if (adev->pm.dpm.uvd_ps)
  780. return adev->pm.dpm.uvd_ps;
  781. else
  782. break;
  783. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  784. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  785. return ps;
  786. break;
  787. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  788. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  789. return ps;
  790. break;
  791. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  792. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  793. return ps;
  794. break;
  795. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  796. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  797. return ps;
  798. break;
  799. case POWER_STATE_TYPE_INTERNAL_BOOT:
  800. return adev->pm.dpm.boot_ps;
  801. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  802. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  803. return ps;
  804. break;
  805. case POWER_STATE_TYPE_INTERNAL_ACPI:
  806. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  807. return ps;
  808. break;
  809. case POWER_STATE_TYPE_INTERNAL_ULV:
  810. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  811. return ps;
  812. break;
  813. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  814. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  815. return ps;
  816. break;
  817. default:
  818. break;
  819. }
  820. }
  821. /* use a fallback state if we didn't match */
  822. switch (dpm_state) {
  823. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  824. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  825. goto restart_search;
  826. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  827. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  828. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  829. if (adev->pm.dpm.uvd_ps) {
  830. return adev->pm.dpm.uvd_ps;
  831. } else {
  832. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  833. goto restart_search;
  834. }
  835. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  836. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  837. goto restart_search;
  838. case POWER_STATE_TYPE_INTERNAL_ACPI:
  839. dpm_state = POWER_STATE_TYPE_BATTERY;
  840. goto restart_search;
  841. case POWER_STATE_TYPE_BATTERY:
  842. case POWER_STATE_TYPE_BALANCED:
  843. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  844. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  845. goto restart_search;
  846. default:
  847. break;
  848. }
  849. return NULL;
  850. }
  851. static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
  852. {
  853. int i;
  854. struct amdgpu_ps *ps;
  855. enum amd_pm_state_type dpm_state;
  856. int ret;
  857. /* if dpm init failed */
  858. if (!adev->pm.dpm_enabled)
  859. return;
  860. if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
  861. /* add other state override checks here */
  862. if ((!adev->pm.dpm.thermal_active) &&
  863. (!adev->pm.dpm.uvd_active))
  864. adev->pm.dpm.state = adev->pm.dpm.user_state;
  865. }
  866. dpm_state = adev->pm.dpm.state;
  867. ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
  868. if (ps)
  869. adev->pm.dpm.requested_ps = ps;
  870. else
  871. return;
  872. /* no need to reprogram if nothing changed unless we are on BTC+ */
  873. if (adev->pm.dpm.current_ps == adev->pm.dpm.requested_ps) {
  874. /* vce just modifies an existing state so force a change */
  875. if (ps->vce_active != adev->pm.dpm.vce_active)
  876. goto force;
  877. if (adev->flags & AMD_IS_APU) {
  878. /* for APUs if the num crtcs changed but state is the same,
  879. * all we need to do is update the display configuration.
  880. */
  881. if (adev->pm.dpm.new_active_crtcs != adev->pm.dpm.current_active_crtcs) {
  882. /* update display watermarks based on new power state */
  883. amdgpu_display_bandwidth_update(adev);
  884. /* update displays */
  885. amdgpu_dpm_display_configuration_changed(adev);
  886. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  887. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  888. }
  889. return;
  890. } else {
  891. /* for BTC+ if the num crtcs hasn't changed and state is the same,
  892. * nothing to do, if the num crtcs is > 1 and state is the same,
  893. * update display configuration.
  894. */
  895. if (adev->pm.dpm.new_active_crtcs ==
  896. adev->pm.dpm.current_active_crtcs) {
  897. return;
  898. } else if ((adev->pm.dpm.current_active_crtc_count > 1) &&
  899. (adev->pm.dpm.new_active_crtc_count > 1)) {
  900. /* update display watermarks based on new power state */
  901. amdgpu_display_bandwidth_update(adev);
  902. /* update displays */
  903. amdgpu_dpm_display_configuration_changed(adev);
  904. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  905. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  906. return;
  907. }
  908. }
  909. }
  910. force:
  911. if (amdgpu_dpm == 1) {
  912. printk("switching from power state:\n");
  913. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
  914. printk("switching to power state:\n");
  915. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
  916. }
  917. /* update whether vce is active */
  918. ps->vce_active = adev->pm.dpm.vce_active;
  919. ret = amdgpu_dpm_pre_set_power_state(adev);
  920. if (ret)
  921. return;
  922. /* update display watermarks based on new power state */
  923. amdgpu_display_bandwidth_update(adev);
  924. /* wait for the rings to drain */
  925. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  926. struct amdgpu_ring *ring = adev->rings[i];
  927. if (ring && ring->ready)
  928. amdgpu_fence_wait_empty(ring);
  929. }
  930. /* program the new power state */
  931. amdgpu_dpm_set_power_state(adev);
  932. /* update current power state */
  933. adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps;
  934. amdgpu_dpm_post_set_power_state(adev);
  935. /* update displays */
  936. amdgpu_dpm_display_configuration_changed(adev);
  937. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  938. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  939. if (adev->pm.funcs->force_performance_level) {
  940. if (adev->pm.dpm.thermal_active) {
  941. enum amdgpu_dpm_forced_level level = adev->pm.dpm.forced_level;
  942. /* force low perf level for thermal */
  943. amdgpu_dpm_force_performance_level(adev, AMDGPU_DPM_FORCED_LEVEL_LOW);
  944. /* save the user's level */
  945. adev->pm.dpm.forced_level = level;
  946. } else {
  947. /* otherwise, user selected level */
  948. amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
  949. }
  950. }
  951. }
  952. void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
  953. {
  954. if (adev->pp_enabled || adev->pm.funcs->powergate_uvd) {
  955. /* enable/disable UVD */
  956. mutex_lock(&adev->pm.mutex);
  957. amdgpu_dpm_powergate_uvd(adev, !enable);
  958. mutex_unlock(&adev->pm.mutex);
  959. } else {
  960. if (enable) {
  961. mutex_lock(&adev->pm.mutex);
  962. adev->pm.dpm.uvd_active = true;
  963. adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
  964. mutex_unlock(&adev->pm.mutex);
  965. } else {
  966. mutex_lock(&adev->pm.mutex);
  967. adev->pm.dpm.uvd_active = false;
  968. mutex_unlock(&adev->pm.mutex);
  969. }
  970. amdgpu_pm_compute_clocks(adev);
  971. }
  972. }
  973. void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
  974. {
  975. if (adev->pp_enabled || adev->pm.funcs->powergate_vce) {
  976. /* enable/disable VCE */
  977. mutex_lock(&adev->pm.mutex);
  978. amdgpu_dpm_powergate_vce(adev, !enable);
  979. mutex_unlock(&adev->pm.mutex);
  980. } else {
  981. if (enable) {
  982. mutex_lock(&adev->pm.mutex);
  983. adev->pm.dpm.vce_active = true;
  984. /* XXX select vce level based on ring/task */
  985. adev->pm.dpm.vce_level = AMDGPU_VCE_LEVEL_AC_ALL;
  986. mutex_unlock(&adev->pm.mutex);
  987. } else {
  988. mutex_lock(&adev->pm.mutex);
  989. adev->pm.dpm.vce_active = false;
  990. mutex_unlock(&adev->pm.mutex);
  991. }
  992. amdgpu_pm_compute_clocks(adev);
  993. }
  994. }
  995. void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
  996. {
  997. int i;
  998. if (adev->pp_enabled)
  999. /* TO DO */
  1000. return;
  1001. for (i = 0; i < adev->pm.dpm.num_ps; i++)
  1002. amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
  1003. }
  1004. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  1005. {
  1006. int ret;
  1007. if (adev->pm.sysfs_initialized)
  1008. return 0;
  1009. if (!adev->pp_enabled) {
  1010. if (adev->pm.funcs->get_temperature == NULL)
  1011. return 0;
  1012. }
  1013. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  1014. DRIVER_NAME, adev,
  1015. hwmon_groups);
  1016. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  1017. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  1018. dev_err(adev->dev,
  1019. "Unable to register hwmon device: %d\n", ret);
  1020. return ret;
  1021. }
  1022. ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
  1023. if (ret) {
  1024. DRM_ERROR("failed to create device file for dpm state\n");
  1025. return ret;
  1026. }
  1027. ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1028. if (ret) {
  1029. DRM_ERROR("failed to create device file for dpm state\n");
  1030. return ret;
  1031. }
  1032. if (adev->pp_enabled) {
  1033. ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
  1034. if (ret) {
  1035. DRM_ERROR("failed to create device file pp_num_states\n");
  1036. return ret;
  1037. }
  1038. ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
  1039. if (ret) {
  1040. DRM_ERROR("failed to create device file pp_cur_state\n");
  1041. return ret;
  1042. }
  1043. ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
  1044. if (ret) {
  1045. DRM_ERROR("failed to create device file pp_force_state\n");
  1046. return ret;
  1047. }
  1048. ret = device_create_file(adev->dev, &dev_attr_pp_table);
  1049. if (ret) {
  1050. DRM_ERROR("failed to create device file pp_table\n");
  1051. return ret;
  1052. }
  1053. }
  1054. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1055. if (ret) {
  1056. DRM_ERROR("failed to create device file pp_dpm_sclk\n");
  1057. return ret;
  1058. }
  1059. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1060. if (ret) {
  1061. DRM_ERROR("failed to create device file pp_dpm_mclk\n");
  1062. return ret;
  1063. }
  1064. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1065. if (ret) {
  1066. DRM_ERROR("failed to create device file pp_dpm_pcie\n");
  1067. return ret;
  1068. }
  1069. ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
  1070. if (ret) {
  1071. DRM_ERROR("failed to create device file pp_sclk_od\n");
  1072. return ret;
  1073. }
  1074. ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
  1075. if (ret) {
  1076. DRM_ERROR("failed to create device file pp_mclk_od\n");
  1077. return ret;
  1078. }
  1079. ret = amdgpu_debugfs_pm_init(adev);
  1080. if (ret) {
  1081. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  1082. return ret;
  1083. }
  1084. adev->pm.sysfs_initialized = true;
  1085. return 0;
  1086. }
  1087. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  1088. {
  1089. if (adev->pm.int_hwmon_dev)
  1090. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  1091. device_remove_file(adev->dev, &dev_attr_power_dpm_state);
  1092. device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1093. if (adev->pp_enabled) {
  1094. device_remove_file(adev->dev, &dev_attr_pp_num_states);
  1095. device_remove_file(adev->dev, &dev_attr_pp_cur_state);
  1096. device_remove_file(adev->dev, &dev_attr_pp_force_state);
  1097. device_remove_file(adev->dev, &dev_attr_pp_table);
  1098. }
  1099. device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1100. device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1101. device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1102. device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
  1103. device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
  1104. }
  1105. void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
  1106. {
  1107. struct drm_device *ddev = adev->ddev;
  1108. struct drm_crtc *crtc;
  1109. struct amdgpu_crtc *amdgpu_crtc;
  1110. if (!adev->pm.dpm_enabled)
  1111. return;
  1112. if (adev->pp_enabled) {
  1113. int i = 0;
  1114. amdgpu_display_bandwidth_update(adev);
  1115. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  1116. struct amdgpu_ring *ring = adev->rings[i];
  1117. if (ring && ring->ready)
  1118. amdgpu_fence_wait_empty(ring);
  1119. }
  1120. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE, NULL, NULL);
  1121. } else {
  1122. mutex_lock(&adev->pm.mutex);
  1123. adev->pm.dpm.new_active_crtcs = 0;
  1124. adev->pm.dpm.new_active_crtc_count = 0;
  1125. if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
  1126. list_for_each_entry(crtc,
  1127. &ddev->mode_config.crtc_list, head) {
  1128. amdgpu_crtc = to_amdgpu_crtc(crtc);
  1129. if (crtc->enabled) {
  1130. adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
  1131. adev->pm.dpm.new_active_crtc_count++;
  1132. }
  1133. }
  1134. }
  1135. /* update battery/ac status */
  1136. if (power_supply_is_system_supplied() > 0)
  1137. adev->pm.dpm.ac_power = true;
  1138. else
  1139. adev->pm.dpm.ac_power = false;
  1140. amdgpu_dpm_change_power_state_locked(adev);
  1141. mutex_unlock(&adev->pm.mutex);
  1142. }
  1143. }
  1144. /*
  1145. * Debugfs info
  1146. */
  1147. #if defined(CONFIG_DEBUG_FS)
  1148. static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
  1149. {
  1150. int32_t value;
  1151. /* sanity check PP is enabled */
  1152. if (!(adev->powerplay.pp_funcs &&
  1153. adev->powerplay.pp_funcs->read_sensor))
  1154. return -EINVAL;
  1155. /* GPU Clocks */
  1156. seq_printf(m, "GFX Clocks and Power:\n");
  1157. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, &value))
  1158. seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
  1159. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, &value))
  1160. seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
  1161. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, &value))
  1162. seq_printf(m, "\t%u mV (VDDGFX)\n", value);
  1163. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, &value))
  1164. seq_printf(m, "\t%u mV (VDDNB)\n", value);
  1165. seq_printf(m, "\n");
  1166. /* GPU Temp */
  1167. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, &value))
  1168. seq_printf(m, "GPU Temperature: %u C\n", value/1000);
  1169. /* GPU Load */
  1170. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value))
  1171. seq_printf(m, "GPU Load: %u %%\n", value);
  1172. seq_printf(m, "\n");
  1173. /* UVD clocks */
  1174. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, &value)) {
  1175. if (!value) {
  1176. seq_printf(m, "UVD: Disabled\n");
  1177. } else {
  1178. seq_printf(m, "UVD: Enabled\n");
  1179. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, &value))
  1180. seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
  1181. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, &value))
  1182. seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
  1183. }
  1184. }
  1185. seq_printf(m, "\n");
  1186. /* VCE clocks */
  1187. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, &value)) {
  1188. if (!value) {
  1189. seq_printf(m, "VCE: Disabled\n");
  1190. } else {
  1191. seq_printf(m, "VCE: Enabled\n");
  1192. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, &value))
  1193. seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
  1194. }
  1195. }
  1196. return 0;
  1197. }
  1198. static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
  1199. {
  1200. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1201. struct drm_device *dev = node->minor->dev;
  1202. struct amdgpu_device *adev = dev->dev_private;
  1203. struct drm_device *ddev = adev->ddev;
  1204. if (!adev->pm.dpm_enabled) {
  1205. seq_printf(m, "dpm not enabled\n");
  1206. return 0;
  1207. }
  1208. if ((adev->flags & AMD_IS_PX) &&
  1209. (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
  1210. seq_printf(m, "PX asic powered off\n");
  1211. } else if (adev->pp_enabled) {
  1212. return amdgpu_debugfs_pm_info_pp(m, adev);
  1213. } else {
  1214. mutex_lock(&adev->pm.mutex);
  1215. if (adev->pm.funcs->debugfs_print_current_performance_level)
  1216. adev->pm.funcs->debugfs_print_current_performance_level(adev, m);
  1217. else
  1218. seq_printf(m, "Debugfs support not implemented for this asic\n");
  1219. mutex_unlock(&adev->pm.mutex);
  1220. }
  1221. return 0;
  1222. }
  1223. static const struct drm_info_list amdgpu_pm_info_list[] = {
  1224. {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
  1225. };
  1226. #endif
  1227. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  1228. {
  1229. #if defined(CONFIG_DEBUG_FS)
  1230. return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
  1231. #else
  1232. return 0;
  1233. #endif
  1234. }