amdgpu_pm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 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. attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  260. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  261. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  262. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  263. return 0;
  264. /* Skip fan attributes if fan is not present */
  265. if (adev->pm.no_fan &&
  266. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  267. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  268. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  269. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  270. return 0;
  271. /* mask fan attributes if we have no bindings for this asic to expose */
  272. if ((!adev->pm.funcs->get_fan_speed_percent &&
  273. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  274. (!adev->pm.funcs->get_fan_control_mode &&
  275. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  276. effective_mode &= ~S_IRUGO;
  277. if ((!adev->pm.funcs->set_fan_speed_percent &&
  278. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  279. (!adev->pm.funcs->set_fan_control_mode &&
  280. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  281. effective_mode &= ~S_IWUSR;
  282. /* hide max/min values if we can't both query and manage the fan */
  283. if ((!adev->pm.funcs->set_fan_speed_percent &&
  284. !adev->pm.funcs->get_fan_speed_percent) &&
  285. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  286. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  287. return 0;
  288. return effective_mode;
  289. }
  290. static const struct attribute_group hwmon_attrgroup = {
  291. .attrs = hwmon_attributes,
  292. .is_visible = hwmon_attributes_visible,
  293. };
  294. static const struct attribute_group *hwmon_groups[] = {
  295. &hwmon_attrgroup,
  296. NULL
  297. };
  298. void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
  299. {
  300. struct amdgpu_device *adev =
  301. container_of(work, struct amdgpu_device,
  302. pm.dpm.thermal.work);
  303. /* switch to the thermal state */
  304. enum amdgpu_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  305. if (!adev->pm.dpm_enabled)
  306. return;
  307. if (adev->pm.funcs->get_temperature) {
  308. int temp = amdgpu_dpm_get_temperature(adev);
  309. if (temp < adev->pm.dpm.thermal.min_temp)
  310. /* switch back the user state */
  311. dpm_state = adev->pm.dpm.user_state;
  312. } else {
  313. if (adev->pm.dpm.thermal.high_to_low)
  314. /* switch back the user state */
  315. dpm_state = adev->pm.dpm.user_state;
  316. }
  317. mutex_lock(&adev->pm.mutex);
  318. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  319. adev->pm.dpm.thermal_active = true;
  320. else
  321. adev->pm.dpm.thermal_active = false;
  322. adev->pm.dpm.state = dpm_state;
  323. mutex_unlock(&adev->pm.mutex);
  324. amdgpu_pm_compute_clocks(adev);
  325. }
  326. static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
  327. enum amdgpu_pm_state_type dpm_state)
  328. {
  329. int i;
  330. struct amdgpu_ps *ps;
  331. u32 ui_class;
  332. bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
  333. true : false;
  334. /* check if the vblank period is too short to adjust the mclk */
  335. if (single_display && adev->pm.funcs->vblank_too_short) {
  336. if (amdgpu_dpm_vblank_too_short(adev))
  337. single_display = false;
  338. }
  339. /* certain older asics have a separare 3D performance state,
  340. * so try that first if the user selected performance
  341. */
  342. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  343. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  344. /* balanced states don't exist at the moment */
  345. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  346. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  347. restart_search:
  348. /* Pick the best power state based on current conditions */
  349. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  350. ps = &adev->pm.dpm.ps[i];
  351. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  352. switch (dpm_state) {
  353. /* user states */
  354. case POWER_STATE_TYPE_BATTERY:
  355. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  356. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  357. if (single_display)
  358. return ps;
  359. } else
  360. return ps;
  361. }
  362. break;
  363. case POWER_STATE_TYPE_BALANCED:
  364. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  365. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  366. if (single_display)
  367. return ps;
  368. } else
  369. return ps;
  370. }
  371. break;
  372. case POWER_STATE_TYPE_PERFORMANCE:
  373. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  374. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  375. if (single_display)
  376. return ps;
  377. } else
  378. return ps;
  379. }
  380. break;
  381. /* internal states */
  382. case POWER_STATE_TYPE_INTERNAL_UVD:
  383. if (adev->pm.dpm.uvd_ps)
  384. return adev->pm.dpm.uvd_ps;
  385. else
  386. break;
  387. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  388. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  389. return ps;
  390. break;
  391. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  392. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  393. return ps;
  394. break;
  395. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  396. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  397. return ps;
  398. break;
  399. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  400. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  401. return ps;
  402. break;
  403. case POWER_STATE_TYPE_INTERNAL_BOOT:
  404. return adev->pm.dpm.boot_ps;
  405. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  406. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  407. return ps;
  408. break;
  409. case POWER_STATE_TYPE_INTERNAL_ACPI:
  410. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  411. return ps;
  412. break;
  413. case POWER_STATE_TYPE_INTERNAL_ULV:
  414. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  415. return ps;
  416. break;
  417. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  418. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  419. return ps;
  420. break;
  421. default:
  422. break;
  423. }
  424. }
  425. /* use a fallback state if we didn't match */
  426. switch (dpm_state) {
  427. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  428. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  429. goto restart_search;
  430. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  431. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  432. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  433. if (adev->pm.dpm.uvd_ps) {
  434. return adev->pm.dpm.uvd_ps;
  435. } else {
  436. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  437. goto restart_search;
  438. }
  439. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  440. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  441. goto restart_search;
  442. case POWER_STATE_TYPE_INTERNAL_ACPI:
  443. dpm_state = POWER_STATE_TYPE_BATTERY;
  444. goto restart_search;
  445. case POWER_STATE_TYPE_BATTERY:
  446. case POWER_STATE_TYPE_BALANCED:
  447. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  448. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  449. goto restart_search;
  450. default:
  451. break;
  452. }
  453. return NULL;
  454. }
  455. static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
  456. {
  457. int i;
  458. struct amdgpu_ps *ps;
  459. enum amdgpu_pm_state_type dpm_state;
  460. int ret;
  461. /* if dpm init failed */
  462. if (!adev->pm.dpm_enabled)
  463. return;
  464. if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
  465. /* add other state override checks here */
  466. if ((!adev->pm.dpm.thermal_active) &&
  467. (!adev->pm.dpm.uvd_active))
  468. adev->pm.dpm.state = adev->pm.dpm.user_state;
  469. }
  470. dpm_state = adev->pm.dpm.state;
  471. ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
  472. if (ps)
  473. adev->pm.dpm.requested_ps = ps;
  474. else
  475. return;
  476. /* no need to reprogram if nothing changed unless we are on BTC+ */
  477. if (adev->pm.dpm.current_ps == adev->pm.dpm.requested_ps) {
  478. /* vce just modifies an existing state so force a change */
  479. if (ps->vce_active != adev->pm.dpm.vce_active)
  480. goto force;
  481. if (adev->flags & AMD_IS_APU) {
  482. /* for APUs if the num crtcs changed but state is the same,
  483. * all we need to do is update the display configuration.
  484. */
  485. if (adev->pm.dpm.new_active_crtcs != adev->pm.dpm.current_active_crtcs) {
  486. /* update display watermarks based on new power state */
  487. amdgpu_display_bandwidth_update(adev);
  488. /* update displays */
  489. amdgpu_dpm_display_configuration_changed(adev);
  490. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  491. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  492. }
  493. return;
  494. } else {
  495. /* for BTC+ if the num crtcs hasn't changed and state is the same,
  496. * nothing to do, if the num crtcs is > 1 and state is the same,
  497. * update display configuration.
  498. */
  499. if (adev->pm.dpm.new_active_crtcs ==
  500. adev->pm.dpm.current_active_crtcs) {
  501. return;
  502. } else if ((adev->pm.dpm.current_active_crtc_count > 1) &&
  503. (adev->pm.dpm.new_active_crtc_count > 1)) {
  504. /* update display watermarks based on new power state */
  505. amdgpu_display_bandwidth_update(adev);
  506. /* update displays */
  507. amdgpu_dpm_display_configuration_changed(adev);
  508. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  509. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  510. return;
  511. }
  512. }
  513. }
  514. force:
  515. if (amdgpu_dpm == 1) {
  516. printk("switching from power state:\n");
  517. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
  518. printk("switching to power state:\n");
  519. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
  520. }
  521. mutex_lock(&adev->ring_lock);
  522. /* update whether vce is active */
  523. ps->vce_active = adev->pm.dpm.vce_active;
  524. ret = amdgpu_dpm_pre_set_power_state(adev);
  525. if (ret)
  526. goto done;
  527. /* update display watermarks based on new power state */
  528. amdgpu_display_bandwidth_update(adev);
  529. /* wait for the rings to drain */
  530. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  531. struct amdgpu_ring *ring = adev->rings[i];
  532. if (ring && ring->ready)
  533. amdgpu_fence_wait_empty(ring);
  534. }
  535. /* program the new power state */
  536. amdgpu_dpm_set_power_state(adev);
  537. /* update current power state */
  538. adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps;
  539. amdgpu_dpm_post_set_power_state(adev);
  540. /* update displays */
  541. amdgpu_dpm_display_configuration_changed(adev);
  542. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  543. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  544. if (adev->pm.funcs->force_performance_level) {
  545. if (adev->pm.dpm.thermal_active) {
  546. enum amdgpu_dpm_forced_level level = adev->pm.dpm.forced_level;
  547. /* force low perf level for thermal */
  548. amdgpu_dpm_force_performance_level(adev, AMDGPU_DPM_FORCED_LEVEL_LOW);
  549. /* save the user's level */
  550. adev->pm.dpm.forced_level = level;
  551. } else {
  552. /* otherwise, user selected level */
  553. amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
  554. }
  555. }
  556. done:
  557. mutex_unlock(&adev->ring_lock);
  558. }
  559. void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
  560. {
  561. if (adev->pm.funcs->powergate_uvd) {
  562. mutex_lock(&adev->pm.mutex);
  563. /* enable/disable UVD */
  564. amdgpu_dpm_powergate_uvd(adev, !enable);
  565. mutex_unlock(&adev->pm.mutex);
  566. } else {
  567. if (enable) {
  568. mutex_lock(&adev->pm.mutex);
  569. adev->pm.dpm.uvd_active = true;
  570. adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
  571. mutex_unlock(&adev->pm.mutex);
  572. } else {
  573. mutex_lock(&adev->pm.mutex);
  574. adev->pm.dpm.uvd_active = false;
  575. mutex_unlock(&adev->pm.mutex);
  576. }
  577. amdgpu_pm_compute_clocks(adev);
  578. }
  579. }
  580. void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
  581. {
  582. if (adev->pm.funcs->powergate_vce) {
  583. mutex_lock(&adev->pm.mutex);
  584. /* enable/disable VCE */
  585. amdgpu_dpm_powergate_vce(adev, !enable);
  586. mutex_unlock(&adev->pm.mutex);
  587. } else {
  588. if (enable) {
  589. mutex_lock(&adev->pm.mutex);
  590. adev->pm.dpm.vce_active = true;
  591. /* XXX select vce level based on ring/task */
  592. adev->pm.dpm.vce_level = AMDGPU_VCE_LEVEL_AC_ALL;
  593. mutex_unlock(&adev->pm.mutex);
  594. } else {
  595. mutex_lock(&adev->pm.mutex);
  596. adev->pm.dpm.vce_active = false;
  597. mutex_unlock(&adev->pm.mutex);
  598. }
  599. amdgpu_pm_compute_clocks(adev);
  600. }
  601. }
  602. void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
  603. {
  604. int i;
  605. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  606. printk("== power state %d ==\n", i);
  607. amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
  608. }
  609. }
  610. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  611. {
  612. int ret;
  613. if (adev->pm.sysfs_initialized)
  614. return 0;
  615. if (adev->pm.funcs->get_temperature == NULL)
  616. return 0;
  617. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  618. DRIVER_NAME, adev,
  619. hwmon_groups);
  620. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  621. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  622. dev_err(adev->dev,
  623. "Unable to register hwmon device: %d\n", ret);
  624. return ret;
  625. }
  626. ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
  627. if (ret) {
  628. DRM_ERROR("failed to create device file for dpm state\n");
  629. return ret;
  630. }
  631. ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  632. if (ret) {
  633. DRM_ERROR("failed to create device file for dpm state\n");
  634. return ret;
  635. }
  636. ret = amdgpu_debugfs_pm_init(adev);
  637. if (ret) {
  638. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  639. return ret;
  640. }
  641. adev->pm.sysfs_initialized = true;
  642. return 0;
  643. }
  644. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  645. {
  646. if (adev->pm.int_hwmon_dev)
  647. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  648. device_remove_file(adev->dev, &dev_attr_power_dpm_state);
  649. device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  650. }
  651. void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
  652. {
  653. struct drm_device *ddev = adev->ddev;
  654. struct drm_crtc *crtc;
  655. struct amdgpu_crtc *amdgpu_crtc;
  656. if (!adev->pm.dpm_enabled)
  657. return;
  658. mutex_lock(&adev->pm.mutex);
  659. /* update active crtc counts */
  660. adev->pm.dpm.new_active_crtcs = 0;
  661. adev->pm.dpm.new_active_crtc_count = 0;
  662. if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
  663. list_for_each_entry(crtc,
  664. &ddev->mode_config.crtc_list, head) {
  665. amdgpu_crtc = to_amdgpu_crtc(crtc);
  666. if (crtc->enabled) {
  667. adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
  668. adev->pm.dpm.new_active_crtc_count++;
  669. }
  670. }
  671. }
  672. /* update battery/ac status */
  673. if (power_supply_is_system_supplied() > 0)
  674. adev->pm.dpm.ac_power = true;
  675. else
  676. adev->pm.dpm.ac_power = false;
  677. amdgpu_dpm_change_power_state_locked(adev);
  678. mutex_unlock(&adev->pm.mutex);
  679. }
  680. /*
  681. * Debugfs info
  682. */
  683. #if defined(CONFIG_DEBUG_FS)
  684. static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
  685. {
  686. struct drm_info_node *node = (struct drm_info_node *) m->private;
  687. struct drm_device *dev = node->minor->dev;
  688. struct amdgpu_device *adev = dev->dev_private;
  689. if (adev->pm.dpm_enabled) {
  690. mutex_lock(&adev->pm.mutex);
  691. if (adev->pm.funcs->debugfs_print_current_performance_level)
  692. amdgpu_dpm_debugfs_print_current_performance_level(adev, m);
  693. else
  694. seq_printf(m, "Debugfs support not implemented for this asic\n");
  695. mutex_unlock(&adev->pm.mutex);
  696. }
  697. return 0;
  698. }
  699. static struct drm_info_list amdgpu_pm_info_list[] = {
  700. {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
  701. };
  702. #endif
  703. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  704. {
  705. #if defined(CONFIG_DEBUG_FS)
  706. return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
  707. #else
  708. return 0;
  709. #endif
  710. }