amdgpu_pm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
  33. void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
  34. {
  35. if (adev->pm.dpm_enabled) {
  36. mutex_lock(&adev->pm.mutex);
  37. if (power_supply_is_system_supplied() > 0)
  38. adev->pm.dpm.ac_power = true;
  39. else
  40. adev->pm.dpm.ac_power = false;
  41. if (adev->pm.funcs->enable_bapm)
  42. amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
  43. mutex_unlock(&adev->pm.mutex);
  44. }
  45. }
  46. static ssize_t amdgpu_get_dpm_state(struct device *dev,
  47. struct device_attribute *attr,
  48. char *buf)
  49. {
  50. struct drm_device *ddev = dev_get_drvdata(dev);
  51. struct amdgpu_device *adev = ddev->dev_private;
  52. enum amdgpu_pm_state_type pm = adev->pm.dpm.user_state;
  53. return snprintf(buf, PAGE_SIZE, "%s\n",
  54. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  55. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  56. }
  57. static ssize_t amdgpu_set_dpm_state(struct device *dev,
  58. struct device_attribute *attr,
  59. const char *buf,
  60. size_t count)
  61. {
  62. struct drm_device *ddev = dev_get_drvdata(dev);
  63. struct amdgpu_device *adev = ddev->dev_private;
  64. mutex_lock(&adev->pm.mutex);
  65. if (strncmp("battery", buf, strlen("battery")) == 0)
  66. adev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
  67. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  68. adev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
  69. else if (strncmp("performance", buf, strlen("performance")) == 0)
  70. adev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
  71. else {
  72. mutex_unlock(&adev->pm.mutex);
  73. count = -EINVAL;
  74. goto fail;
  75. }
  76. mutex_unlock(&adev->pm.mutex);
  77. /* Can't set dpm state when the card is off */
  78. if (!(adev->flags & AMD_IS_PX) ||
  79. (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
  80. amdgpu_pm_compute_clocks(adev);
  81. fail:
  82. return count;
  83. }
  84. static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
  85. struct device_attribute *attr,
  86. char *buf)
  87. {
  88. struct drm_device *ddev = dev_get_drvdata(dev);
  89. struct amdgpu_device *adev = ddev->dev_private;
  90. enum amdgpu_dpm_forced_level level = adev->pm.dpm.forced_level;
  91. return snprintf(buf, PAGE_SIZE, "%s\n",
  92. (level == AMDGPU_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  93. (level == AMDGPU_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
  94. }
  95. static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
  96. struct device_attribute *attr,
  97. const char *buf,
  98. size_t count)
  99. {
  100. struct drm_device *ddev = dev_get_drvdata(dev);
  101. struct amdgpu_device *adev = ddev->dev_private;
  102. enum amdgpu_dpm_forced_level level;
  103. int ret = 0;
  104. mutex_lock(&adev->pm.mutex);
  105. if (strncmp("low", buf, strlen("low")) == 0) {
  106. level = AMDGPU_DPM_FORCED_LEVEL_LOW;
  107. } else if (strncmp("high", buf, strlen("high")) == 0) {
  108. level = AMDGPU_DPM_FORCED_LEVEL_HIGH;
  109. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  110. level = AMDGPU_DPM_FORCED_LEVEL_AUTO;
  111. } else {
  112. count = -EINVAL;
  113. goto fail;
  114. }
  115. if (adev->pm.funcs->force_performance_level) {
  116. if (adev->pm.dpm.thermal_active) {
  117. count = -EINVAL;
  118. goto fail;
  119. }
  120. ret = amdgpu_dpm_force_performance_level(adev, level);
  121. if (ret)
  122. count = -EINVAL;
  123. }
  124. fail:
  125. mutex_unlock(&adev->pm.mutex);
  126. return count;
  127. }
  128. static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
  129. static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
  130. amdgpu_get_dpm_forced_performance_level,
  131. amdgpu_set_dpm_forced_performance_level);
  132. static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
  133. struct device_attribute *attr,
  134. char *buf)
  135. {
  136. struct amdgpu_device *adev = dev_get_drvdata(dev);
  137. int temp;
  138. if (adev->pm.funcs->get_temperature)
  139. temp = amdgpu_dpm_get_temperature(adev);
  140. else
  141. temp = 0;
  142. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  143. }
  144. static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
  145. struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct amdgpu_device *adev = dev_get_drvdata(dev);
  149. int hyst = to_sensor_dev_attr(attr)->index;
  150. int temp;
  151. if (hyst)
  152. temp = adev->pm.dpm.thermal.min_temp;
  153. else
  154. temp = adev->pm.dpm.thermal.max_temp;
  155. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  156. }
  157. static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
  158. struct device_attribute *attr,
  159. char *buf)
  160. {
  161. struct amdgpu_device *adev = dev_get_drvdata(dev);
  162. u32 pwm_mode = 0;
  163. if (adev->pm.funcs->get_fan_control_mode)
  164. pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
  165. /* never 0 (full-speed), fuse or smc-controlled always */
  166. return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
  167. }
  168. static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
  169. struct device_attribute *attr,
  170. const char *buf,
  171. size_t count)
  172. {
  173. struct amdgpu_device *adev = dev_get_drvdata(dev);
  174. int err;
  175. int value;
  176. if(!adev->pm.funcs->set_fan_control_mode)
  177. return -EINVAL;
  178. err = kstrtoint(buf, 10, &value);
  179. if (err)
  180. return err;
  181. switch (value) {
  182. case 1: /* manual, percent-based */
  183. amdgpu_dpm_set_fan_control_mode(adev, FDO_PWM_MODE_STATIC);
  184. break;
  185. default: /* disable */
  186. amdgpu_dpm_set_fan_control_mode(adev, 0);
  187. break;
  188. }
  189. return count;
  190. }
  191. static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
  192. struct device_attribute *attr,
  193. char *buf)
  194. {
  195. return sprintf(buf, "%i\n", 0);
  196. }
  197. static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
  198. struct device_attribute *attr,
  199. char *buf)
  200. {
  201. return sprintf(buf, "%i\n", 255);
  202. }
  203. static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
  204. struct device_attribute *attr,
  205. const char *buf, size_t count)
  206. {
  207. struct amdgpu_device *adev = dev_get_drvdata(dev);
  208. int err;
  209. u32 value;
  210. err = kstrtou32(buf, 10, &value);
  211. if (err)
  212. return err;
  213. value = (value * 100) / 255;
  214. err = amdgpu_dpm_set_fan_speed_percent(adev, value);
  215. if (err)
  216. return err;
  217. return count;
  218. }
  219. static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
  220. struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct amdgpu_device *adev = dev_get_drvdata(dev);
  224. int err;
  225. u32 speed;
  226. err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
  227. if (err)
  228. return err;
  229. speed = (speed * 255) / 100;
  230. return sprintf(buf, "%i\n", speed);
  231. }
  232. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
  233. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
  234. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
  235. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
  236. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
  237. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
  238. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
  239. static struct attribute *hwmon_attributes[] = {
  240. &sensor_dev_attr_temp1_input.dev_attr.attr,
  241. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  242. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  243. &sensor_dev_attr_pwm1.dev_attr.attr,
  244. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  245. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  246. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  247. NULL
  248. };
  249. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  250. struct attribute *attr, int index)
  251. {
  252. struct device *dev = container_of(kobj, struct device, kobj);
  253. struct amdgpu_device *adev = dev_get_drvdata(dev);
  254. umode_t effective_mode = attr->mode;
  255. /* Skip limit attributes if DPM is not enabled */
  256. if (!adev->pm.dpm_enabled &&
  257. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  258. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
  259. return 0;
  260. /* Skip fan attributes if fan is not present */
  261. if (adev->pm.no_fan &&
  262. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  263. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  264. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  265. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  266. return 0;
  267. /* mask fan attributes if we have no bindings for this asic to expose */
  268. if ((!adev->pm.funcs->get_fan_speed_percent &&
  269. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  270. (!adev->pm.funcs->get_fan_control_mode &&
  271. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  272. effective_mode &= ~S_IRUGO;
  273. if ((!adev->pm.funcs->set_fan_speed_percent &&
  274. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  275. (!adev->pm.funcs->set_fan_control_mode &&
  276. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  277. effective_mode &= ~S_IWUSR;
  278. /* hide max/min values if we can't both query and manage the fan */
  279. if ((!adev->pm.funcs->set_fan_speed_percent &&
  280. !adev->pm.funcs->get_fan_speed_percent) &&
  281. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  282. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  283. return 0;
  284. return effective_mode;
  285. }
  286. static const struct attribute_group hwmon_attrgroup = {
  287. .attrs = hwmon_attributes,
  288. .is_visible = hwmon_attributes_visible,
  289. };
  290. static const struct attribute_group *hwmon_groups[] = {
  291. &hwmon_attrgroup,
  292. NULL
  293. };
  294. void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
  295. {
  296. struct amdgpu_device *adev =
  297. container_of(work, struct amdgpu_device,
  298. pm.dpm.thermal.work);
  299. /* switch to the thermal state */
  300. enum amdgpu_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  301. if (!adev->pm.dpm_enabled)
  302. return;
  303. if (adev->pm.funcs->get_temperature) {
  304. int temp = amdgpu_dpm_get_temperature(adev);
  305. if (temp < adev->pm.dpm.thermal.min_temp)
  306. /* switch back the user state */
  307. dpm_state = adev->pm.dpm.user_state;
  308. } else {
  309. if (adev->pm.dpm.thermal.high_to_low)
  310. /* switch back the user state */
  311. dpm_state = adev->pm.dpm.user_state;
  312. }
  313. mutex_lock(&adev->pm.mutex);
  314. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  315. adev->pm.dpm.thermal_active = true;
  316. else
  317. adev->pm.dpm.thermal_active = false;
  318. adev->pm.dpm.state = dpm_state;
  319. mutex_unlock(&adev->pm.mutex);
  320. amdgpu_pm_compute_clocks(adev);
  321. }
  322. static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
  323. enum amdgpu_pm_state_type dpm_state)
  324. {
  325. int i;
  326. struct amdgpu_ps *ps;
  327. u32 ui_class;
  328. bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
  329. true : false;
  330. /* check if the vblank period is too short to adjust the mclk */
  331. if (single_display && adev->pm.funcs->vblank_too_short) {
  332. if (amdgpu_dpm_vblank_too_short(adev))
  333. single_display = false;
  334. }
  335. /* certain older asics have a separare 3D performance state,
  336. * so try that first if the user selected performance
  337. */
  338. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  339. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  340. /* balanced states don't exist at the moment */
  341. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  342. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  343. restart_search:
  344. /* Pick the best power state based on current conditions */
  345. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  346. ps = &adev->pm.dpm.ps[i];
  347. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  348. switch (dpm_state) {
  349. /* user states */
  350. case POWER_STATE_TYPE_BATTERY:
  351. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  352. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  353. if (single_display)
  354. return ps;
  355. } else
  356. return ps;
  357. }
  358. break;
  359. case POWER_STATE_TYPE_BALANCED:
  360. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  361. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  362. if (single_display)
  363. return ps;
  364. } else
  365. return ps;
  366. }
  367. break;
  368. case POWER_STATE_TYPE_PERFORMANCE:
  369. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  370. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  371. if (single_display)
  372. return ps;
  373. } else
  374. return ps;
  375. }
  376. break;
  377. /* internal states */
  378. case POWER_STATE_TYPE_INTERNAL_UVD:
  379. if (adev->pm.dpm.uvd_ps)
  380. return adev->pm.dpm.uvd_ps;
  381. else
  382. break;
  383. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  384. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  385. return ps;
  386. break;
  387. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  388. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  389. return ps;
  390. break;
  391. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  392. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  393. return ps;
  394. break;
  395. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  396. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  397. return ps;
  398. break;
  399. case POWER_STATE_TYPE_INTERNAL_BOOT:
  400. return adev->pm.dpm.boot_ps;
  401. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  402. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  403. return ps;
  404. break;
  405. case POWER_STATE_TYPE_INTERNAL_ACPI:
  406. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  407. return ps;
  408. break;
  409. case POWER_STATE_TYPE_INTERNAL_ULV:
  410. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  411. return ps;
  412. break;
  413. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  414. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  415. return ps;
  416. break;
  417. default:
  418. break;
  419. }
  420. }
  421. /* use a fallback state if we didn't match */
  422. switch (dpm_state) {
  423. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  424. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  425. goto restart_search;
  426. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  427. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  428. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  429. if (adev->pm.dpm.uvd_ps) {
  430. return adev->pm.dpm.uvd_ps;
  431. } else {
  432. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  433. goto restart_search;
  434. }
  435. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  436. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  437. goto restart_search;
  438. case POWER_STATE_TYPE_INTERNAL_ACPI:
  439. dpm_state = POWER_STATE_TYPE_BATTERY;
  440. goto restart_search;
  441. case POWER_STATE_TYPE_BATTERY:
  442. case POWER_STATE_TYPE_BALANCED:
  443. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  444. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  445. goto restart_search;
  446. default:
  447. break;
  448. }
  449. return NULL;
  450. }
  451. static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
  452. {
  453. int i;
  454. struct amdgpu_ps *ps;
  455. enum amdgpu_pm_state_type dpm_state;
  456. int ret;
  457. /* if dpm init failed */
  458. if (!adev->pm.dpm_enabled)
  459. return;
  460. if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
  461. /* add other state override checks here */
  462. if ((!adev->pm.dpm.thermal_active) &&
  463. (!adev->pm.dpm.uvd_active))
  464. adev->pm.dpm.state = adev->pm.dpm.user_state;
  465. }
  466. dpm_state = adev->pm.dpm.state;
  467. ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
  468. if (ps)
  469. adev->pm.dpm.requested_ps = ps;
  470. else
  471. return;
  472. /* no need to reprogram if nothing changed unless we are on BTC+ */
  473. if (adev->pm.dpm.current_ps == adev->pm.dpm.requested_ps) {
  474. /* vce just modifies an existing state so force a change */
  475. if (ps->vce_active != adev->pm.dpm.vce_active)
  476. goto force;
  477. if (adev->flags & AMD_IS_APU) {
  478. /* for APUs if the num crtcs changed but state is the same,
  479. * all we need to do is update the display configuration.
  480. */
  481. if (adev->pm.dpm.new_active_crtcs != adev->pm.dpm.current_active_crtcs) {
  482. /* update display watermarks based on new power state */
  483. amdgpu_display_bandwidth_update(adev);
  484. /* update displays */
  485. amdgpu_dpm_display_configuration_changed(adev);
  486. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  487. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  488. }
  489. return;
  490. } else {
  491. /* for BTC+ if the num crtcs hasn't changed and state is the same,
  492. * nothing to do, if the num crtcs is > 1 and state is the same,
  493. * update display configuration.
  494. */
  495. if (adev->pm.dpm.new_active_crtcs ==
  496. adev->pm.dpm.current_active_crtcs) {
  497. return;
  498. } else if ((adev->pm.dpm.current_active_crtc_count > 1) &&
  499. (adev->pm.dpm.new_active_crtc_count > 1)) {
  500. /* update display watermarks based on new power state */
  501. amdgpu_display_bandwidth_update(adev);
  502. /* update displays */
  503. amdgpu_dpm_display_configuration_changed(adev);
  504. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  505. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  506. return;
  507. }
  508. }
  509. }
  510. force:
  511. if (amdgpu_dpm == 1) {
  512. printk("switching from power state:\n");
  513. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
  514. printk("switching to power state:\n");
  515. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
  516. }
  517. mutex_lock(&adev->ddev->struct_mutex);
  518. mutex_lock(&adev->ring_lock);
  519. /* update whether vce is active */
  520. ps->vce_active = adev->pm.dpm.vce_active;
  521. ret = amdgpu_dpm_pre_set_power_state(adev);
  522. if (ret)
  523. goto done;
  524. /* update display watermarks based on new power state */
  525. amdgpu_display_bandwidth_update(adev);
  526. /* update displays */
  527. amdgpu_dpm_display_configuration_changed(adev);
  528. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  529. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  530. /* wait for the rings to drain */
  531. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  532. struct amdgpu_ring *ring = adev->rings[i];
  533. if (ring && ring->ready)
  534. amdgpu_fence_wait_empty(ring);
  535. }
  536. /* program the new power state */
  537. amdgpu_dpm_set_power_state(adev);
  538. /* update current power state */
  539. adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps;
  540. amdgpu_dpm_post_set_power_state(adev);
  541. if (adev->pm.funcs->force_performance_level) {
  542. if (adev->pm.dpm.thermal_active) {
  543. enum amdgpu_dpm_forced_level level = adev->pm.dpm.forced_level;
  544. /* force low perf level for thermal */
  545. amdgpu_dpm_force_performance_level(adev, AMDGPU_DPM_FORCED_LEVEL_LOW);
  546. /* save the user's level */
  547. adev->pm.dpm.forced_level = level;
  548. } else {
  549. /* otherwise, user selected level */
  550. amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
  551. }
  552. }
  553. done:
  554. mutex_unlock(&adev->ring_lock);
  555. mutex_unlock(&adev->ddev->struct_mutex);
  556. }
  557. void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
  558. {
  559. if (adev->pm.funcs->powergate_uvd) {
  560. mutex_lock(&adev->pm.mutex);
  561. /* enable/disable UVD */
  562. amdgpu_dpm_powergate_uvd(adev, !enable);
  563. mutex_unlock(&adev->pm.mutex);
  564. } else {
  565. if (enable) {
  566. mutex_lock(&adev->pm.mutex);
  567. adev->pm.dpm.uvd_active = true;
  568. adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
  569. mutex_unlock(&adev->pm.mutex);
  570. } else {
  571. mutex_lock(&adev->pm.mutex);
  572. adev->pm.dpm.uvd_active = false;
  573. mutex_unlock(&adev->pm.mutex);
  574. }
  575. amdgpu_pm_compute_clocks(adev);
  576. }
  577. }
  578. void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
  579. {
  580. if (adev->pm.funcs->powergate_vce) {
  581. mutex_lock(&adev->pm.mutex);
  582. /* enable/disable VCE */
  583. amdgpu_dpm_powergate_vce(adev, !enable);
  584. mutex_unlock(&adev->pm.mutex);
  585. } else {
  586. if (enable) {
  587. mutex_lock(&adev->pm.mutex);
  588. adev->pm.dpm.vce_active = true;
  589. /* XXX select vce level based on ring/task */
  590. adev->pm.dpm.vce_level = AMDGPU_VCE_LEVEL_AC_ALL;
  591. mutex_unlock(&adev->pm.mutex);
  592. } else {
  593. mutex_lock(&adev->pm.mutex);
  594. adev->pm.dpm.vce_active = false;
  595. mutex_unlock(&adev->pm.mutex);
  596. }
  597. amdgpu_pm_compute_clocks(adev);
  598. }
  599. }
  600. void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
  601. {
  602. int i;
  603. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  604. printk("== power state %d ==\n", i);
  605. amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
  606. }
  607. }
  608. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  609. {
  610. int ret;
  611. if (adev->pm.funcs->get_temperature == NULL)
  612. return 0;
  613. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  614. DRIVER_NAME, adev,
  615. hwmon_groups);
  616. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  617. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  618. dev_err(adev->dev,
  619. "Unable to register hwmon device: %d\n", ret);
  620. return ret;
  621. }
  622. ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
  623. if (ret) {
  624. DRM_ERROR("failed to create device file for dpm state\n");
  625. return ret;
  626. }
  627. ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  628. if (ret) {
  629. DRM_ERROR("failed to create device file for dpm state\n");
  630. return ret;
  631. }
  632. ret = amdgpu_debugfs_pm_init(adev);
  633. if (ret) {
  634. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  635. return ret;
  636. }
  637. return 0;
  638. }
  639. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  640. {
  641. if (adev->pm.int_hwmon_dev)
  642. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  643. device_remove_file(adev->dev, &dev_attr_power_dpm_state);
  644. device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  645. }
  646. void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
  647. {
  648. struct drm_device *ddev = adev->ddev;
  649. struct drm_crtc *crtc;
  650. struct amdgpu_crtc *amdgpu_crtc;
  651. if (!adev->pm.dpm_enabled)
  652. return;
  653. mutex_lock(&adev->pm.mutex);
  654. /* update active crtc counts */
  655. adev->pm.dpm.new_active_crtcs = 0;
  656. adev->pm.dpm.new_active_crtc_count = 0;
  657. if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
  658. list_for_each_entry(crtc,
  659. &ddev->mode_config.crtc_list, head) {
  660. amdgpu_crtc = to_amdgpu_crtc(crtc);
  661. if (crtc->enabled) {
  662. adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
  663. adev->pm.dpm.new_active_crtc_count++;
  664. }
  665. }
  666. }
  667. /* update battery/ac status */
  668. if (power_supply_is_system_supplied() > 0)
  669. adev->pm.dpm.ac_power = true;
  670. else
  671. adev->pm.dpm.ac_power = false;
  672. amdgpu_dpm_change_power_state_locked(adev);
  673. mutex_unlock(&adev->pm.mutex);
  674. }
  675. /*
  676. * Debugfs info
  677. */
  678. #if defined(CONFIG_DEBUG_FS)
  679. static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
  680. {
  681. struct drm_info_node *node = (struct drm_info_node *) m->private;
  682. struct drm_device *dev = node->minor->dev;
  683. struct amdgpu_device *adev = dev->dev_private;
  684. if (adev->pm.dpm_enabled) {
  685. mutex_lock(&adev->pm.mutex);
  686. if (adev->pm.funcs->debugfs_print_current_performance_level)
  687. amdgpu_dpm_debugfs_print_current_performance_level(adev, m);
  688. else
  689. seq_printf(m, "Debugfs support not implemented for this asic\n");
  690. mutex_unlock(&adev->pm.mutex);
  691. }
  692. return 0;
  693. }
  694. static struct drm_info_list amdgpu_pm_info_list[] = {
  695. {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
  696. };
  697. #endif
  698. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  699. {
  700. #if defined(CONFIG_DEBUG_FS)
  701. return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
  702. #else
  703. return 0;
  704. #endif
  705. }