radeon_pm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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 "drmP.h"
  24. #include "radeon.h"
  25. #include "radeon_acpi.h"
  26. #include "avivod.h"
  27. #include "atom.h"
  28. #ifdef CONFIG_ACPI
  29. #include <linux/acpi.h>
  30. #endif
  31. #include <linux/power_supply.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. #define RADEON_IDLE_LOOP_MS 100
  35. #define RADEON_RECLOCK_DELAY_MS 200
  36. #define RADEON_WAIT_VBLANK_TIMEOUT 200
  37. static const char *radeon_pm_state_type_name[5] = {
  38. "Default",
  39. "Powersave",
  40. "Battery",
  41. "Balanced",
  42. "Performance",
  43. };
  44. static void radeon_dynpm_idle_work_handler(struct work_struct *work);
  45. static int radeon_debugfs_pm_init(struct radeon_device *rdev);
  46. static bool radeon_pm_in_vbl(struct radeon_device *rdev);
  47. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
  48. static void radeon_pm_update_profile(struct radeon_device *rdev);
  49. static void radeon_pm_set_clocks(struct radeon_device *rdev);
  50. #define ACPI_AC_CLASS "ac_adapter"
  51. int radeon_pm_get_type_index(struct radeon_device *rdev,
  52. enum radeon_pm_state_type ps_type,
  53. int instance)
  54. {
  55. int i;
  56. int found_instance = -1;
  57. for (i = 0; i < rdev->pm.num_power_states; i++) {
  58. if (rdev->pm.power_state[i].type == ps_type) {
  59. found_instance++;
  60. if (found_instance == instance)
  61. return i;
  62. }
  63. }
  64. /* return default if no match */
  65. return rdev->pm.default_power_state_index;
  66. }
  67. #ifdef CONFIG_ACPI
  68. static int radeon_acpi_event(struct notifier_block *nb,
  69. unsigned long val,
  70. void *data)
  71. {
  72. struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
  73. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  74. if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
  75. if (power_supply_is_system_supplied() > 0)
  76. DRM_DEBUG_DRIVER("pm: AC\n");
  77. else
  78. DRM_DEBUG_DRIVER("pm: DC\n");
  79. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  80. if (rdev->pm.profile == PM_PROFILE_AUTO) {
  81. mutex_lock(&rdev->pm.mutex);
  82. radeon_pm_update_profile(rdev);
  83. radeon_pm_set_clocks(rdev);
  84. mutex_unlock(&rdev->pm.mutex);
  85. }
  86. }
  87. }
  88. /* Check for pending SBIOS requests */
  89. return radeon_atif_handler(rdev, entry);
  90. }
  91. #endif
  92. static void radeon_pm_update_profile(struct radeon_device *rdev)
  93. {
  94. switch (rdev->pm.profile) {
  95. case PM_PROFILE_DEFAULT:
  96. rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
  97. break;
  98. case PM_PROFILE_AUTO:
  99. if (power_supply_is_system_supplied() > 0) {
  100. if (rdev->pm.active_crtc_count > 1)
  101. rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
  102. else
  103. rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
  104. } else {
  105. if (rdev->pm.active_crtc_count > 1)
  106. rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
  107. else
  108. rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
  109. }
  110. break;
  111. case PM_PROFILE_LOW:
  112. if (rdev->pm.active_crtc_count > 1)
  113. rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
  114. else
  115. rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
  116. break;
  117. case PM_PROFILE_MID:
  118. if (rdev->pm.active_crtc_count > 1)
  119. rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
  120. else
  121. rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
  122. break;
  123. case PM_PROFILE_HIGH:
  124. if (rdev->pm.active_crtc_count > 1)
  125. rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
  126. else
  127. rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
  128. break;
  129. }
  130. if (rdev->pm.active_crtc_count == 0) {
  131. rdev->pm.requested_power_state_index =
  132. rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
  133. rdev->pm.requested_clock_mode_index =
  134. rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
  135. } else {
  136. rdev->pm.requested_power_state_index =
  137. rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
  138. rdev->pm.requested_clock_mode_index =
  139. rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
  140. }
  141. }
  142. static void radeon_unmap_vram_bos(struct radeon_device *rdev)
  143. {
  144. struct radeon_bo *bo, *n;
  145. if (list_empty(&rdev->gem.objects))
  146. return;
  147. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  148. if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
  149. ttm_bo_unmap_virtual(&bo->tbo);
  150. }
  151. }
  152. static void radeon_sync_with_vblank(struct radeon_device *rdev)
  153. {
  154. if (rdev->pm.active_crtcs) {
  155. rdev->pm.vblank_sync = false;
  156. wait_event_timeout(
  157. rdev->irq.vblank_queue, rdev->pm.vblank_sync,
  158. msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
  159. }
  160. }
  161. static void radeon_set_power_state(struct radeon_device *rdev)
  162. {
  163. u32 sclk, mclk;
  164. bool misc_after = false;
  165. if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
  166. (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
  167. return;
  168. if (radeon_gui_idle(rdev)) {
  169. sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
  170. clock_info[rdev->pm.requested_clock_mode_index].sclk;
  171. if (sclk > rdev->pm.default_sclk)
  172. sclk = rdev->pm.default_sclk;
  173. mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
  174. clock_info[rdev->pm.requested_clock_mode_index].mclk;
  175. if (mclk > rdev->pm.default_mclk)
  176. mclk = rdev->pm.default_mclk;
  177. /* upvolt before raising clocks, downvolt after lowering clocks */
  178. if (sclk < rdev->pm.current_sclk)
  179. misc_after = true;
  180. radeon_sync_with_vblank(rdev);
  181. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  182. if (!radeon_pm_in_vbl(rdev))
  183. return;
  184. }
  185. radeon_pm_prepare(rdev);
  186. if (!misc_after)
  187. /* voltage, pcie lanes, etc.*/
  188. radeon_pm_misc(rdev);
  189. /* set engine clock */
  190. if (sclk != rdev->pm.current_sclk) {
  191. radeon_pm_debug_check_in_vbl(rdev, false);
  192. radeon_set_engine_clock(rdev, sclk);
  193. radeon_pm_debug_check_in_vbl(rdev, true);
  194. rdev->pm.current_sclk = sclk;
  195. DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
  196. }
  197. /* set memory clock */
  198. if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
  199. radeon_pm_debug_check_in_vbl(rdev, false);
  200. radeon_set_memory_clock(rdev, mclk);
  201. radeon_pm_debug_check_in_vbl(rdev, true);
  202. rdev->pm.current_mclk = mclk;
  203. DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
  204. }
  205. if (misc_after)
  206. /* voltage, pcie lanes, etc.*/
  207. radeon_pm_misc(rdev);
  208. radeon_pm_finish(rdev);
  209. rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
  210. rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
  211. } else
  212. DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
  213. }
  214. static void radeon_pm_set_clocks(struct radeon_device *rdev)
  215. {
  216. int i;
  217. /* no need to take locks, etc. if nothing's going to change */
  218. if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
  219. (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
  220. return;
  221. mutex_lock(&rdev->ddev->struct_mutex);
  222. down_write(&rdev->pm.mclk_lock);
  223. mutex_lock(&rdev->ring_lock);
  224. /* wait for the rings to drain */
  225. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  226. struct radeon_ring *ring = &rdev->ring[i];
  227. if (ring->ready)
  228. radeon_fence_wait_empty_locked(rdev, i);
  229. }
  230. radeon_unmap_vram_bos(rdev);
  231. if (rdev->irq.installed) {
  232. for (i = 0; i < rdev->num_crtc; i++) {
  233. if (rdev->pm.active_crtcs & (1 << i)) {
  234. rdev->pm.req_vblank |= (1 << i);
  235. drm_vblank_get(rdev->ddev, i);
  236. }
  237. }
  238. }
  239. radeon_set_power_state(rdev);
  240. if (rdev->irq.installed) {
  241. for (i = 0; i < rdev->num_crtc; i++) {
  242. if (rdev->pm.req_vblank & (1 << i)) {
  243. rdev->pm.req_vblank &= ~(1 << i);
  244. drm_vblank_put(rdev->ddev, i);
  245. }
  246. }
  247. }
  248. /* update display watermarks based on new power state */
  249. radeon_update_bandwidth_info(rdev);
  250. if (rdev->pm.active_crtc_count)
  251. radeon_bandwidth_update(rdev);
  252. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  253. mutex_unlock(&rdev->ring_lock);
  254. up_write(&rdev->pm.mclk_lock);
  255. mutex_unlock(&rdev->ddev->struct_mutex);
  256. }
  257. static void radeon_pm_print_states(struct radeon_device *rdev)
  258. {
  259. int i, j;
  260. struct radeon_power_state *power_state;
  261. struct radeon_pm_clock_info *clock_info;
  262. DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
  263. for (i = 0; i < rdev->pm.num_power_states; i++) {
  264. power_state = &rdev->pm.power_state[i];
  265. DRM_DEBUG_DRIVER("State %d: %s\n", i,
  266. radeon_pm_state_type_name[power_state->type]);
  267. if (i == rdev->pm.default_power_state_index)
  268. DRM_DEBUG_DRIVER("\tDefault");
  269. if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
  270. DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
  271. if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
  272. DRM_DEBUG_DRIVER("\tSingle display only\n");
  273. DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
  274. for (j = 0; j < power_state->num_clock_modes; j++) {
  275. clock_info = &(power_state->clock_info[j]);
  276. if (rdev->flags & RADEON_IS_IGP)
  277. DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
  278. j,
  279. clock_info->sclk * 10,
  280. clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
  281. else
  282. DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
  283. j,
  284. clock_info->sclk * 10,
  285. clock_info->mclk * 10,
  286. clock_info->voltage.voltage,
  287. clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
  288. }
  289. }
  290. }
  291. static ssize_t radeon_get_pm_profile(struct device *dev,
  292. struct device_attribute *attr,
  293. char *buf)
  294. {
  295. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  296. struct radeon_device *rdev = ddev->dev_private;
  297. int cp = rdev->pm.profile;
  298. return snprintf(buf, PAGE_SIZE, "%s\n",
  299. (cp == PM_PROFILE_AUTO) ? "auto" :
  300. (cp == PM_PROFILE_LOW) ? "low" :
  301. (cp == PM_PROFILE_MID) ? "mid" :
  302. (cp == PM_PROFILE_HIGH) ? "high" : "default");
  303. }
  304. static ssize_t radeon_set_pm_profile(struct device *dev,
  305. struct device_attribute *attr,
  306. const char *buf,
  307. size_t count)
  308. {
  309. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  310. struct radeon_device *rdev = ddev->dev_private;
  311. mutex_lock(&rdev->pm.mutex);
  312. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  313. if (strncmp("default", buf, strlen("default")) == 0)
  314. rdev->pm.profile = PM_PROFILE_DEFAULT;
  315. else if (strncmp("auto", buf, strlen("auto")) == 0)
  316. rdev->pm.profile = PM_PROFILE_AUTO;
  317. else if (strncmp("low", buf, strlen("low")) == 0)
  318. rdev->pm.profile = PM_PROFILE_LOW;
  319. else if (strncmp("mid", buf, strlen("mid")) == 0)
  320. rdev->pm.profile = PM_PROFILE_MID;
  321. else if (strncmp("high", buf, strlen("high")) == 0)
  322. rdev->pm.profile = PM_PROFILE_HIGH;
  323. else {
  324. count = -EINVAL;
  325. goto fail;
  326. }
  327. radeon_pm_update_profile(rdev);
  328. radeon_pm_set_clocks(rdev);
  329. } else
  330. count = -EINVAL;
  331. fail:
  332. mutex_unlock(&rdev->pm.mutex);
  333. return count;
  334. }
  335. static ssize_t radeon_get_pm_method(struct device *dev,
  336. struct device_attribute *attr,
  337. char *buf)
  338. {
  339. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  340. struct radeon_device *rdev = ddev->dev_private;
  341. int pm = rdev->pm.pm_method;
  342. return snprintf(buf, PAGE_SIZE, "%s\n",
  343. (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
  344. }
  345. static ssize_t radeon_set_pm_method(struct device *dev,
  346. struct device_attribute *attr,
  347. const char *buf,
  348. size_t count)
  349. {
  350. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  351. struct radeon_device *rdev = ddev->dev_private;
  352. if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
  353. mutex_lock(&rdev->pm.mutex);
  354. rdev->pm.pm_method = PM_METHOD_DYNPM;
  355. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  356. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  357. mutex_unlock(&rdev->pm.mutex);
  358. } else if (strncmp("profile", buf, strlen("profile")) == 0) {
  359. mutex_lock(&rdev->pm.mutex);
  360. /* disable dynpm */
  361. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  362. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  363. rdev->pm.pm_method = PM_METHOD_PROFILE;
  364. mutex_unlock(&rdev->pm.mutex);
  365. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  366. } else {
  367. count = -EINVAL;
  368. goto fail;
  369. }
  370. radeon_pm_compute_clocks(rdev);
  371. fail:
  372. return count;
  373. }
  374. static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
  375. static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
  376. static ssize_t radeon_hwmon_show_temp(struct device *dev,
  377. struct device_attribute *attr,
  378. char *buf)
  379. {
  380. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  381. struct radeon_device *rdev = ddev->dev_private;
  382. int temp;
  383. switch (rdev->pm.int_thermal_type) {
  384. case THERMAL_TYPE_RV6XX:
  385. temp = rv6xx_get_temp(rdev);
  386. break;
  387. case THERMAL_TYPE_RV770:
  388. temp = rv770_get_temp(rdev);
  389. break;
  390. case THERMAL_TYPE_EVERGREEN:
  391. case THERMAL_TYPE_NI:
  392. temp = evergreen_get_temp(rdev);
  393. break;
  394. case THERMAL_TYPE_SUMO:
  395. temp = sumo_get_temp(rdev);
  396. break;
  397. case THERMAL_TYPE_SI:
  398. temp = si_get_temp(rdev);
  399. break;
  400. default:
  401. temp = 0;
  402. break;
  403. }
  404. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  405. }
  406. static ssize_t radeon_hwmon_show_name(struct device *dev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. return sprintf(buf, "radeon\n");
  411. }
  412. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
  413. static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
  414. static struct attribute *hwmon_attributes[] = {
  415. &sensor_dev_attr_temp1_input.dev_attr.attr,
  416. &sensor_dev_attr_name.dev_attr.attr,
  417. NULL
  418. };
  419. static const struct attribute_group hwmon_attrgroup = {
  420. .attrs = hwmon_attributes,
  421. };
  422. static int radeon_hwmon_init(struct radeon_device *rdev)
  423. {
  424. int err = 0;
  425. rdev->pm.int_hwmon_dev = NULL;
  426. switch (rdev->pm.int_thermal_type) {
  427. case THERMAL_TYPE_RV6XX:
  428. case THERMAL_TYPE_RV770:
  429. case THERMAL_TYPE_EVERGREEN:
  430. case THERMAL_TYPE_NI:
  431. case THERMAL_TYPE_SUMO:
  432. case THERMAL_TYPE_SI:
  433. /* No support for TN yet */
  434. if (rdev->family == CHIP_ARUBA)
  435. return err;
  436. rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
  437. if (IS_ERR(rdev->pm.int_hwmon_dev)) {
  438. err = PTR_ERR(rdev->pm.int_hwmon_dev);
  439. dev_err(rdev->dev,
  440. "Unable to register hwmon device: %d\n", err);
  441. break;
  442. }
  443. dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
  444. err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
  445. &hwmon_attrgroup);
  446. if (err) {
  447. dev_err(rdev->dev,
  448. "Unable to create hwmon sysfs file: %d\n", err);
  449. hwmon_device_unregister(rdev->dev);
  450. }
  451. break;
  452. default:
  453. break;
  454. }
  455. return err;
  456. }
  457. static void radeon_hwmon_fini(struct radeon_device *rdev)
  458. {
  459. if (rdev->pm.int_hwmon_dev) {
  460. sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
  461. hwmon_device_unregister(rdev->pm.int_hwmon_dev);
  462. }
  463. }
  464. void radeon_pm_suspend(struct radeon_device *rdev)
  465. {
  466. mutex_lock(&rdev->pm.mutex);
  467. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  468. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
  469. rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
  470. }
  471. mutex_unlock(&rdev->pm.mutex);
  472. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  473. }
  474. void radeon_pm_resume(struct radeon_device *rdev)
  475. {
  476. /* set up the default clocks if the MC ucode is loaded */
  477. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  478. if (rdev->pm.default_vddc)
  479. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  480. SET_VOLTAGE_TYPE_ASIC_VDDC);
  481. if (rdev->pm.default_vddci)
  482. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  483. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  484. if (rdev->pm.default_sclk)
  485. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  486. if (rdev->pm.default_mclk)
  487. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  488. }
  489. /* asic init will reset the default power state */
  490. mutex_lock(&rdev->pm.mutex);
  491. rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
  492. rdev->pm.current_clock_mode_index = 0;
  493. rdev->pm.current_sclk = rdev->pm.default_sclk;
  494. rdev->pm.current_mclk = rdev->pm.default_mclk;
  495. rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
  496. rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
  497. if (rdev->pm.pm_method == PM_METHOD_DYNPM
  498. && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
  499. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  500. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  501. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  502. }
  503. mutex_unlock(&rdev->pm.mutex);
  504. radeon_pm_compute_clocks(rdev);
  505. }
  506. int radeon_pm_init(struct radeon_device *rdev)
  507. {
  508. int ret;
  509. /* default to profile method */
  510. rdev->pm.pm_method = PM_METHOD_PROFILE;
  511. rdev->pm.profile = PM_PROFILE_DEFAULT;
  512. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  513. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  514. rdev->pm.dynpm_can_upclock = true;
  515. rdev->pm.dynpm_can_downclock = true;
  516. rdev->pm.default_sclk = rdev->clock.default_sclk;
  517. rdev->pm.default_mclk = rdev->clock.default_mclk;
  518. rdev->pm.current_sclk = rdev->clock.default_sclk;
  519. rdev->pm.current_mclk = rdev->clock.default_mclk;
  520. rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  521. if (rdev->bios) {
  522. if (rdev->is_atom_bios)
  523. radeon_atombios_get_power_modes(rdev);
  524. else
  525. radeon_combios_get_power_modes(rdev);
  526. radeon_pm_print_states(rdev);
  527. radeon_pm_init_profile(rdev);
  528. /* set up the default clocks if the MC ucode is loaded */
  529. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  530. if (rdev->pm.default_vddc)
  531. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  532. SET_VOLTAGE_TYPE_ASIC_VDDC);
  533. if (rdev->pm.default_vddci)
  534. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  535. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  536. if (rdev->pm.default_sclk)
  537. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  538. if (rdev->pm.default_mclk)
  539. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  540. }
  541. }
  542. /* set up the internal thermal sensor if applicable */
  543. ret = radeon_hwmon_init(rdev);
  544. if (ret)
  545. return ret;
  546. INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
  547. if (rdev->pm.num_power_states > 1) {
  548. /* where's the best place to put these? */
  549. ret = device_create_file(rdev->dev, &dev_attr_power_profile);
  550. if (ret)
  551. DRM_ERROR("failed to create device file for power profile\n");
  552. ret = device_create_file(rdev->dev, &dev_attr_power_method);
  553. if (ret)
  554. DRM_ERROR("failed to create device file for power method\n");
  555. #ifdef CONFIG_ACPI
  556. rdev->acpi_nb.notifier_call = radeon_acpi_event;
  557. register_acpi_notifier(&rdev->acpi_nb);
  558. #endif
  559. if (radeon_debugfs_pm_init(rdev)) {
  560. DRM_ERROR("Failed to register debugfs file for PM!\n");
  561. }
  562. DRM_INFO("radeon: power management initialized\n");
  563. }
  564. return 0;
  565. }
  566. void radeon_pm_fini(struct radeon_device *rdev)
  567. {
  568. if (rdev->pm.num_power_states > 1) {
  569. mutex_lock(&rdev->pm.mutex);
  570. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  571. rdev->pm.profile = PM_PROFILE_DEFAULT;
  572. radeon_pm_update_profile(rdev);
  573. radeon_pm_set_clocks(rdev);
  574. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  575. /* reset default clocks */
  576. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  577. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  578. radeon_pm_set_clocks(rdev);
  579. }
  580. mutex_unlock(&rdev->pm.mutex);
  581. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  582. device_remove_file(rdev->dev, &dev_attr_power_profile);
  583. device_remove_file(rdev->dev, &dev_attr_power_method);
  584. #ifdef CONFIG_ACPI
  585. unregister_acpi_notifier(&rdev->acpi_nb);
  586. #endif
  587. }
  588. if (rdev->pm.power_state)
  589. kfree(rdev->pm.power_state);
  590. radeon_hwmon_fini(rdev);
  591. }
  592. void radeon_pm_compute_clocks(struct radeon_device *rdev)
  593. {
  594. struct drm_device *ddev = rdev->ddev;
  595. struct drm_crtc *crtc;
  596. struct radeon_crtc *radeon_crtc;
  597. if (rdev->pm.num_power_states < 2)
  598. return;
  599. mutex_lock(&rdev->pm.mutex);
  600. rdev->pm.active_crtcs = 0;
  601. rdev->pm.active_crtc_count = 0;
  602. list_for_each_entry(crtc,
  603. &ddev->mode_config.crtc_list, head) {
  604. radeon_crtc = to_radeon_crtc(crtc);
  605. if (radeon_crtc->enabled) {
  606. rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
  607. rdev->pm.active_crtc_count++;
  608. }
  609. }
  610. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  611. radeon_pm_update_profile(rdev);
  612. radeon_pm_set_clocks(rdev);
  613. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  614. if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
  615. if (rdev->pm.active_crtc_count > 1) {
  616. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  617. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  618. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  619. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  620. radeon_pm_get_dynpm_state(rdev);
  621. radeon_pm_set_clocks(rdev);
  622. DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
  623. }
  624. } else if (rdev->pm.active_crtc_count == 1) {
  625. /* TODO: Increase clocks if needed for current mode */
  626. if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
  627. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  628. rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
  629. radeon_pm_get_dynpm_state(rdev);
  630. radeon_pm_set_clocks(rdev);
  631. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  632. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  633. } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
  634. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  635. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  636. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  637. DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
  638. }
  639. } else { /* count == 0 */
  640. if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
  641. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  642. rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
  643. rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
  644. radeon_pm_get_dynpm_state(rdev);
  645. radeon_pm_set_clocks(rdev);
  646. }
  647. }
  648. }
  649. }
  650. mutex_unlock(&rdev->pm.mutex);
  651. }
  652. static bool radeon_pm_in_vbl(struct radeon_device *rdev)
  653. {
  654. int crtc, vpos, hpos, vbl_status;
  655. bool in_vbl = true;
  656. /* Iterate over all active crtc's. All crtc's must be in vblank,
  657. * otherwise return in_vbl == false.
  658. */
  659. for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
  660. if (rdev->pm.active_crtcs & (1 << crtc)) {
  661. vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
  662. if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
  663. !(vbl_status & DRM_SCANOUTPOS_INVBL))
  664. in_vbl = false;
  665. }
  666. }
  667. return in_vbl;
  668. }
  669. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
  670. {
  671. u32 stat_crtc = 0;
  672. bool in_vbl = radeon_pm_in_vbl(rdev);
  673. if (in_vbl == false)
  674. DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
  675. finish ? "exit" : "entry");
  676. return in_vbl;
  677. }
  678. static void radeon_dynpm_idle_work_handler(struct work_struct *work)
  679. {
  680. struct radeon_device *rdev;
  681. int resched;
  682. rdev = container_of(work, struct radeon_device,
  683. pm.dynpm_idle_work.work);
  684. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  685. mutex_lock(&rdev->pm.mutex);
  686. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  687. int not_processed = 0;
  688. int i;
  689. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  690. struct radeon_ring *ring = &rdev->ring[i];
  691. if (ring->ready) {
  692. not_processed += radeon_fence_count_emitted(rdev, i);
  693. if (not_processed >= 3)
  694. break;
  695. }
  696. }
  697. if (not_processed >= 3) { /* should upclock */
  698. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
  699. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  700. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  701. rdev->pm.dynpm_can_upclock) {
  702. rdev->pm.dynpm_planned_action =
  703. DYNPM_ACTION_UPCLOCK;
  704. rdev->pm.dynpm_action_timeout = jiffies +
  705. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  706. }
  707. } else if (not_processed == 0) { /* should downclock */
  708. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
  709. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  710. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  711. rdev->pm.dynpm_can_downclock) {
  712. rdev->pm.dynpm_planned_action =
  713. DYNPM_ACTION_DOWNCLOCK;
  714. rdev->pm.dynpm_action_timeout = jiffies +
  715. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  716. }
  717. }
  718. /* Note, radeon_pm_set_clocks is called with static_switch set
  719. * to false since we want to wait for vbl to avoid flicker.
  720. */
  721. if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
  722. jiffies > rdev->pm.dynpm_action_timeout) {
  723. radeon_pm_get_dynpm_state(rdev);
  724. radeon_pm_set_clocks(rdev);
  725. }
  726. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  727. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  728. }
  729. mutex_unlock(&rdev->pm.mutex);
  730. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  731. }
  732. /*
  733. * Debugfs info
  734. */
  735. #if defined(CONFIG_DEBUG_FS)
  736. static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
  737. {
  738. struct drm_info_node *node = (struct drm_info_node *) m->private;
  739. struct drm_device *dev = node->minor->dev;
  740. struct radeon_device *rdev = dev->dev_private;
  741. seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
  742. seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
  743. seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
  744. if (rdev->asic->pm.get_memory_clock)
  745. seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
  746. if (rdev->pm.current_vddc)
  747. seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
  748. if (rdev->asic->pm.get_pcie_lanes)
  749. seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
  750. return 0;
  751. }
  752. static struct drm_info_list radeon_pm_info_list[] = {
  753. {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
  754. };
  755. #endif
  756. static int radeon_debugfs_pm_init(struct radeon_device *rdev)
  757. {
  758. #if defined(CONFIG_DEBUG_FS)
  759. return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
  760. #else
  761. return 0;
  762. #endif
  763. }